Archive for 'Corona'

Saving Score to a Text File in Corona

In Corona, it is possible to save scores to an external text file. This can be accomplished using the following lines of code :

This code snippet will save the score value in the scoreStr variable to the the file score.txt.

Common properties for touch events in Corona.

event.phase

In the case of a “touch” event, there are four different possible phases for each touch:

  • began – the touch begins (the moment a touch happens)
  • moved – the user moves/drags their finger (but does not lift it)
  • ended – when the user lifts their finger from the screen
  • cancelled – if the system stops tracking the touch before it officially ends (e.g. object is removed while still being touched, focus is put on a different object, etc)

event.id

This is a unique identifier given to most events which can be used to identify similar events separately. The most common use for this property is detecting separate touches on the same object when multi-touch is enabled.

event.target

This is probably the most common event property, and is used to reference the object that actually triggered the event.

event.x and event.y

If your event corresponds to specific locations on the screen, such as the location of touches during a “touch” event, the coordinates can be accessed via the event.x and event.y properties. Some others include event.xStart andevent.yStart (which are the coordinates for where the touches began).

 

Incoming search terms:

  • touch events an arduino
  • android touchevent an arduino

system.orientation

Description:
Returns the current device orientation. This property will return one of six possible string values, from a set consisting of four orientations plus faceUp and faceDown.

Note: On Android, the orientation values are limited to portrait and landscapeRight.

Related Items:

Orientation Event

Syntax:
1
system.orientation
Example:
print( system.orientation ) — print the system orientation
Parameters:
None.

Returns:
String — current orientation.

The six possible returned values are:
“portrait”
“portraitUpsideDown”
“landscapeRight”
“landscapeLeft”
“faceUp”
“faceDown”

Activity Indicator in Corona

The following code is used to create activity indicator in Corona.
1 native.setActivityIndicator( true);
Based on the platform in which the app is run the nature of the activity indicator changes automatically.

To remove the activity indicator, use the following code.
1 native.setActivityIndicator( false);

Syntax :

display.fps

Example :

print(display.fps)  –> current frame rate (eg: 30)

Flip/Mirror tips

If you want to flip and/or mirror a display object, scale it negatively :

local img = display.newImage(“image.png”, 0, 0);

img:scale(-1, 1); – Mirror

img:scale(1, -1); – Flip

Implementing Lightning Effects in Corona

As it turns out, generating lightning between two endpoints can be a deceptively simple thing to generate.  It can be generated as an L-System (with some randomization per generation).  Some simple pseudo-code follows:

segmentList.Add(new Segment(startPoint, endPoint));

offsetAmount = maximumOffset; // the maximum amount to offset a lightning vertex.

for each generation (some number of generations)

for each segment that was in segmentList when this generation started

segmentList.Remove(segment); // This segment is no longer necessary.

midPoint = Average(startpoint, endPoint);

// Offset the midpoint by a random amount along the normal.

midPoint += Perpendicular(Normalize(endPoint-startPoint))*RandomFloat(-offsetAmount,offsetAmount);

// Create two new segments that span from the start point to the end point,

// but with the new (randomly-offset) midpoint.

segmentList.Add(new Segment(startPoint, midPoint));

segmentList.Add(new Segment(midPoint, endPoint));

end for

offsetAmount /= 2; // Each subsequent generation offsets at max half as much as the generation before.

end for

Essentially, on each generation, subdivide each line segment into two, and offset the new point a little bit.  Each generation has half of the offset that the previous had.

Parse for Corona SDK

This  shows how to communicate with Parse.com, a back-end data service, from a Corona SDK app. You should be able to adapt it to your needs with minimal changes. See main.lua for a barebones UI.

To use the parse.lua module, add:

Parse = require(“parse”)

to your main project file and refer to functions as shown in main.lua.

Color Transition Wrapper

Since changing color smoothly seems like a task that pops up often during game development,  to make a wrapper function (around Corona’s default transition.to) in order to make it easier.

TO USE :
First load the table that contains the function:

1.cTrans = require(“colortransition”)

Then call the wrapper function as such:

cTrans:colorTransition(displayObject, colorFrom, colorTo, [time], [delay], [easing]) ;

i.e.

cTrans:colorTransition(rect, {255,255,255} , {0,0,0}, 500)
Sets a transition on rect that goes from white to black in 500 ms.

Orbital rotation

local background = display.newImage( “space.png”, true )

background.x = display.contentWidth / 2

background.y = display.contentHeight / 2

local planet1 = display.newImage( “earth.png” )

planet1.x = 160; planet1.y = 230

planet1.xScale = 0.7

planet1.yScale = 0.7

physics.addBody( planet1, {radius=40 } )

planet1.bodyType = “static”

local moon = display.newImage( “moon.png”, 40, 40)

moon.x = planet1.x + 80

moon.y = planet1.y

moon.xScale = 0.3

moon.yScale = 0.3

moon.rotation = 100

physics.addBody( moon, {radius=16 } )

moon.bodyType = “dynamic”

myJoint = physics.newJoint( “pivot”, moon, planet1, planet1.x, planet1.y )

local rotateIt = function( event )

moon.rotation = moon.rotation + 1.5

end

Runtime:addEventListener( “enterFrame”, rotateIt )

Page 1 of 1412345...10...Last »