Tile based games with JS
Hmm, this looks pretty interesting. There seems to be a lot of talk of games in HTML5 these days.
Hmm, this looks pretty interesting. There seems to be a lot of talk of games in HTML5 these days.
May be not. Do you need self control? Maybe. Self control is app that blocks incoming and outgoing mail servers for a predetermined period of time. It effectively lets you turn off email, twitter and facebook updates until you’re done with what you are working on.
Here’s an article on all of the ins and out of using the browser developer tools. If you want to make web sites this information will only make it easier!
Corona has a nifty physics engine built in. The physics engine is used to simulate the interactions of objects using real world effects like gravity and motion. Elements, called “bodies”, can properties like density, friction and bounciness, which make them behave like real objects in the real world. Imagine you threw a ball. It might fly through the air while gravity pulled it toward the earth. Then it might bounce when it hit the ground and bounce again when it a wall. If you wanted to create a similar effect in an app the physics engine would help you do this and make it look realistic.
Using physics in Corona is surprisingly easy and straight forward. The first step is to import the physics class and turn the physics engine on.
local physics = require("physics")
physics.start()
The first line import the physics engine and the second turns it on. The first line stores a reference to the physics engine in a variable named “physics”, we’ll use this to talk to the physics engine. Notice the second line uses the “physics” variable to turn the engine on. As we go you’ll see we’ll use the name “physics” to create physics “bodies” and set other options having to do with the physics environment.
Create a body
Next create a display object of some kind. This can be an image or a shape, then create a physics “body” from that object. The body is controlled by the physics environment and essentially takes over the display object moving it as it might move with the effects of gravity and bumping into other “bodies”.
Here’s a simple example that creates an image turns it into a physics body.
local box = display.newImage("box.png")
physics.addBody( box, "dynamic", { density=1.0, friction=0.3, bounce=0.65 } )
Here we call physics.addBody() with three parameters. The first, a display object to create the body from, box in this example. The second sets the type of object this will, “dynamic” in this case. “Dynamic” objects can move around and bump into other object. Last is a table containing options for the object, density, friction and bounce in this example.
Try it for yourself
Try this out for yourself. Follow the instructions below.
local physics = require("physics")
physics.setDrawMode( "debug" )
physics.start()
Here I imported the physics library and set some options. I set the draw mode to debug. This will display simple shapes for your physics objects. Second I started the physics engine running.
Now add a shape. Add a small image to your folder to use a physics object. Make this a square or a circle about 20px in diameter.
local ball = display.newImage("ball.png")
ball.x = 160
ball.y = 30
physics.addBody( ball, "dynamic",{ density=1.0, friction=0, bounce=0.65, radius=10 } )
Here you imported your image and made a display object from it, with the name “ball”. Then positioned the ball at the top center of the screen. Then you created a physics “body” from your display object. This is a “dynamic” object so it will move around. I added the “radius” property and set the value to the radius of the ball image. Since my ball was 20px I used 10 as the radius. Adding radius makes the “body” circular.
Test your project. At this point the ball should fall down the screen as if it were pulled by the force of gravity. The same thing would happen if you dropped a real ball. Of course a real ball would not fall forever it would hit the ground at some point.
Make a static body
Next we’ll create a ground plane.
local floor = display.newRect(0, 0, 320, 10)
floor.x = 160
floor.y = 475
floor:setFillColor( 255, 0, 0 )
physics.addBody( floor, "static", {density=1.0, friction=0.3 } )
Rather than using an image this time I created a rectangle shape object, 320px wide and 10px tall. The new shape is named “floor”. Next we positioned the floor at the bottom center of the screen. Then give it a fill color of red. Last we create a new “body” from the floor shape. Here the type is “static”. Static shapes don’t move.
Test your project again. This time the ball falls and bounces off the ground. Change the bounce value assigned to the ball body, notice the change.
Gravity
Change the gravity in your physics world with physics.setGravity(x,y). Gravity is a constant force that pulls object in one direction. Add the following to your main.lua.
physics.setGravity(0,0)
Test your project again, notice the ball goes nowhere. This time there is no gravity. We can use this setting to simulate a top down view. Try out a few different values for the x and y of gravity. Set this back to 0 and 0 when you’re satisfied you understand what is happening with gravity.
Add more walls
Add three more walls to create a box. Add the following to your project file.
local ceiling = display.newRect(0, 0, 320, 10)
ceiling.x = 160
ceiling.y = 5
ceiling:setFillColor( 255, 0, 0 )
physics.addBody( ceiling, "static", {density=1.0, friction=0.3 } )
local left = display.newRect(0, 0, 10, 480)
left.x = 5
left.y = 240
left:setFillColor( 255, 0, 0 )
physics.addBody( left, "static", {density=1.0, friction=0.3 } )
local right = display.newRect(0, 0, 10, 480)
right.x = 315
right.y = 240
right:setFillColor( 255, 0, 0 )
physics.addBody( right, "static", {density=1.0, friction=0.3 } )
Here I’ve added three more boxes like the floor. These make up the left and right walls and the ceiling.
Test your project again. You should see the new walls and there should be a wall on each edge of the screen.
Velocity
Next we’ll make the ball move by tapping the screen. With each tap we’ll give the ball some linear velocity. That’s velocity in a straight line.
local function onTouch(event)
local x = ball.x - event.x
local y = ball.y - event.y
ball:setLinearVelocity( x, y )
end
Runtime:addEventListener("touch", onTouch)
Here I created a function, onTouch. This function is triggered when the screen is touched. The event object passed to the function contains an x and a y position of where the touch occurred. The first to lines in the function the difference between the touch x and y and the ball x and y. The last line calls the setLinearVelocity method of ball and uses the two values to push the ball away from were you touch the screen.
The line below the function adds an event listener to the screen. This is what will trigger the event when a touch occurs.
Test your project again. This time the ball should sit still until you touch the screen. When you touch the screen the ball should move way from the point where the touch occurred bouncing off the walls as it moves.
Here’s a few things to try for yourself.
Sprites are similar to movieclips but more efficient. Where movieclips use a separate image for each frame of an animation. A sprite uses one single image that contains all of the frames. Mobile devices have limited memory and images take up most of the memory your projects might use. Using a single image where you once had several can dramatically cut down the amount of memory your app may be using.
A sprite sheet is a single image that contains all of the individual images that make up an animated sequence of images. Here’s an example. This image contains three animated images of the saucer (the four spots in the center move). Each individual image is 48pixels wide by 21 pixels tall. I used a transparent png, where the red was transparent, in my original test. I had to fill in the transparent area or you would not be able to see the white image against the page here.
A sprite sheet can have more than one series of images. As a matter of fact you could have all of the images used in your project inside of a single sprite sheet. Note the iPhone has a maximum image size that is can work with. On earlier iPhones the max size is 1024 by 1024 while newer models support up to 2048 by 2048 pixels.
The artwork for this example is more difficult to create than the other examples you’ll need to pay close attention to how it is created and sized. You can use the example image above if you like.
Add the following code to your main.lus file. You’ll need to change the file name of saucer_sprites.png to the name of your image file.
You’ll also need to change 48 to the width of your sprites and 21 to the height. These are the width and height of each individual frames.
-- Include the sprite
require "sprite"
-- name the image file containing your sprites, followed by the width and height of each sprite
local saucer_sheet = sprite.newSpriteSheet("saucer_sprites.png", 48, 21)
-- Create a set by naming the sheet and starting frame and number of frames
local saucer_sprite = sprite.newSpriteSet(saucer_sheet, 1, 3)
-- Now create new sprite, give it a name, set the starting frame, end frame,
-- and time between frames in ms, 0 means loop
sprite.add( saucer_sprite, "saucer1", 1, 3, 100, 0 )
-- Create a new Sprite in the display object so we can see it on the screen
local saucer = sprite.newSprite( saucer_sprite )
-- Position your sprite
saucer.x = display.contentWidth / 2
saucer.y = display.contentHeight / 2
-- Prep your sprite
saucer:prepare("saucer1")
-- Start the sprite playing
saucer:play()