In Part 1 of the tutorial, we set the environment for our game. Now it is time to set the physics, the timer and the interactions.
Setting the physics environment
Go to Toolset Physics and click Physics Environment. Accept the following data:
Creating bodies
We need to set bodies to the following layers: ball, balloons, floor, thrower and ramp. As the last 3 ones have the same characteristics, select all 3 of them and click Set body properties:
Enter the above info. All 3 layers are Static bodies, with rectangular shapes. Clicking Save creates the 3 bodies.
Select the balloons layer and click Set body properties:
Do the same for layer ball:
Creating actions
We need 3 main actions in our project:
- act_addSec: this action reduces the content of the timer in one second;
- act_hitBalloon: called when the ball hits the balloon. It will reset the game;
- act_upSeconds: starts the timer, when user clicks the PLAY button
Back in the Toolset Project and Pages, click the Actions button. When it opens, name it act_addSec, select option Code, Edit Variable. Enter the following:
Save the action.
Click the Actions button again. Now, create the act_upSeconds. Select option Timers, then Start Timer. Enter the following:
What it will do is to play the action act_addSec for 5 times (this way, the timer counter will count backwards, from 5 to 0). Save the action.
Click the Actions button again. Now, create the act_hitBalloon. Select option Code, then External Code. Enter the following code, when the window opens:
local function delayBalloons() — creates a function called delayBalloons
balloons.bodyType = “dynamic” — set the body of layer balloons to dynamic
physics.setGravity(0,-9.8) — invert gravity to -9,8 (objects will float, instead of fall)
end — ends the function
timer.performWithDelay(10,delayBalloons) — start the function delayBalloons after 0.01 seconds
timerStash.timer_134 = timer.performWithDelay(5000,restartScreen, 1) –re-start the timer for the next play
Setting the collision between the ball and the balloon
The next step is to check when the ball hits the balloon. Back to the Toolset Physics, click Set Collisions:
In this case, we are triggering the action act_hitBallon when the ball hits the balloons. Save it.
Creating the Play button
The Play button re-start everything and allows the player to reset the ramp in 5 seconds, before the ball falls again from the thrower. Select the layer button. Back in the Toolset Interactions, click Add Button. This button will have four actions:
1) Code->External Code: restart positions of the ball and the balloon. Copy and paste the following code:
local function restartScreen()
physics.setGravity(0, 9.8) — returns the gravity to 0,9.8 (objects will fall now)
ball.bodyType = “static” — ball becomes static
ball.y = -130; ball.x = 512 — and returns to its original position at 512,-130
balloons.bodyType = “static” — balloon becomes static
balloons.rotation = 0 — with no rotation (original shape)
balloons.x = math.random( 0, 1024); balloons.y = 635 — being positioned randomly in the X axis, but fixed at Y axis at 635 (height of the floor layer)
local function delayBall()
ball.bodyType = “dynamic” — makes the ball a dynamic object again
end
timer.performWithDelay(5000,delayBall) — waits 5 seconds, then start function delayBall
end
timer.performWithDelay( 10, restartScreen ) — after 0.01 seconds, calls the restartScreen function above
2) Code->Edit Variable: “zeroes” the seconds variable to 5 (time in seconds for the player to play with the ramp)
3) Common->Play Action: re-start the act_upSeconds action (which controls the display of the timer)
4) Animation->Play Animation: plays animation simulating the thrower being expanded while the ball passes thru it
Previewing your project
Make sure you have the Export images icon highlighted in the Status bar, then press the Publish button. Kwik will create all images and code necessary for previewing your project. When it finishes, the Simulator starts.
As you can see, there are great benefits integrating external code into Kwik. Enjoy!