[code]require “CiderDebugger”;
—Storyboard---------
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
–Global Variables
local parallaxNode = nil
local player = nil
local rocketFlame = nil
local playerVelocity = 0
local playerWasFlying = false
local playerShouldFly = false
local globalGroup = nil
–Score-----------------------------
local score = 0;
function scoreHitAtPosition(position, points)
score = score + points;
print ("Score: " … tostring(score))
end
–Mouse Coin Collision-------------------------
local function mouseCoinCollision(event)
–print("Sprite " … event.object1.lhUniqueName … " will collide with " … event.object2.lhUniqueName)
local coin = event.spriteB;
if(nil ~= coin) then
if(coin.alpha == 1.0) then
scoreHitAtPosition({x = coin.x, y = coin.y}, 100);
end
coin.alpha = 0.0;
end
end
—Player Is Dead----------------------------------
local playerIsDead = false;
local function killPlayer()
playerVelocity = 0.0;
playerShouldFly = false;
playerIsDead = true;
playerWasFlying = false;
rocketFlame.alpha = 0;
player:prepareAnimationNamed(“mouseDie”);
player:playAnimation();
parallaxNode:setSpeed(0);
local gameOverText = display.newText(“Game Over!”, 0, 0, native.systemFont, 32)
gameOverText:setTextColor(255, 0, 0)
gameOverText.x = display.viewableContentWidth / 2
gameOverText.y = display.viewableContentHeight /2 - 40
local restartText = display.newText(“Restart”, 0, 0, native.systemFont, 24)
restartText:setTextColor(255, 255, 255)
restartText.x = display.viewableContentWidth / 2
restartText.y = display.viewableContentHeight /2
local onRestartTouch = function(event)
if(event.phase == “began”)then
local storyboard = require(“storyboard”)
storyboard.gotoScene(“reset”)
Runtime:removeEventListener(“touch”, restartText);
end
end
restartText:addEventListener(“touch”, onRestartTouch)
globalGroup:insert(“gameOverText”)
globalGroup:insert(“restartText”)
end
–Lasers Collison---------------------------------------
local lasersThatNeedHandling = {};
local function mouseLaserCollision(event)
–print("Sprite " … event.object1.lhUniqueName … " is going collide with " … event.object2.lhUniqueName)
local laser = event.spriteB;
if(playerIsDead == true)then
return;
end
if(laser:currentAnimationFrame() ~= 1)then
killPlayer();
end
– If we make the laser a sensor, the callback will be called only once - at first collision.
– This is not good as we want to kill the player when the laser changes to active.
– So we make the laser sensor, so that the player and laser don’t collide, durring this collision.
– Then we keep track of the lasers that needs to change back to not being sensors
– and in the enterFrame we set back the lasers to isSensor = false
laser.isSensor = true;
lasersThatNeedHandling[#lasersThatNeedHandling+1] = laser;
end
----Start Collision---------------------------
local function setupCollisionHandling()
loader:registerBeginOrEndCollisionCallbackBetweenTags(LevelHelper_TAG.PLAYER,
LevelHelper_TAG.COIN,mouseCoinCollision)
loader:registerPreCollisionCallbackBetweenTags(LevelHelper_TAG.PLAYER, LevelHelper_TAG.LASER, mouseLaserCollision);
end
–Start Player Fly-----------------------------------------
function startPlayerFly()
if(playerIsDead == true)then
return
end
–print(“Start player fly”);
playerVelocity = 0.5
playerShouldFly = true
rocketFlame.alpha = 1
player:prepareAnimationNamed(“mouseFly”)
player:playAnimation()
end
–Cancel Player Fly----------------------
function cancelPlayerFly()
–print(“Cancel player fly”);
playerShouldFly = false
rocketFlame.alpha = 0
playerWasFlying = true
player:prepareAnimationNamed(“mouseFall”)
player:playAnimation()
playerVelocity = playerVelocity - 0.01
if(playerVelocity < 0) then
playerVelocity = 0
end
end
–Player Fly Function--------------------------------------
local function onTouch(event)
if(event.phase == “began”) then
startPlayerFly()
elseif(event.phase == “ended” or event.phase == “cancelled”) then
cancelPlayerFly()
end
end
–Player Impluse Function-----------------------------------------------
local onEnterFrame = function(event)
if(playerShouldFly == true) then
player:applyLinearImpulse(0, -playerVelocity, player.x, player.y)
playerVelocity = playerVelocity + 0.01
if(playerVelocity > 1.0) then
playerVelocity = 1.0
end
end
for i=1, #lasersThatNeedHandling do
lasersThatNeedHandling[i].isSensor = false;
end
lasersThatNeedHandling = {};
end
—Storyboard Enter Scene----------------------------
function scene:enterScene(event)
globalGroup = self.view
—Physics---------------------
local ui = require (“ui”)
local physics = require(“physics”)
local fps = require(“fps”)
physics.start()
–comment next line to disable debug drawing
–physics.setDrawMode( “hybrid” )
display.setStatusBar( display.HiddenStatusBar )
physics.setGravity(0, 10);
–Variables-------
parallaxNode = nil
player = nil
rocketFlame = nil
playerVelocity = 0;
playerIsDead = false;
playerWasFlying = false;
playerShouldFly = false;
lasersThatNeedHandling = nil
lasersThatNeedHandling = {};
score = 0;
require(“LevelHelper.LevelHelperLoader”)
application.LevelHelperSettings.directorGroup = self.view;
loader = LevelHelperLoader:initWithContentOfFile(“level03.plhs”)
loader:instantiateSprites();
parallaxNode = loader:parallaxWithUniqueName(“parallax1”);
if(nil == parallaxNode) then
print (“Could not find parallax.”)
end
player = loader:spriteWithUniqueName(“player”)
if(nil == player) then
print (“Could not find player.”)
end
rocketFlame = loader:spriteWithUniqueName(“flame”)
if(nil == rocketFlame) then
print (“Could not find rocket flame.”)
end
rocketFlame.alpha = 0
loader:useLevelHelperCollisionHandling()–instructs LH to take care of collisions
setupCollisionHandling()
Runtime:addEventListener(“touch”, onTouch)
Runtime:addEventListener(“enterFrame”, onEnterFrame)
end
function scene:exitScene(event)
local group = self.view
loader:removeSelf()
loader = nil;
player = nil;
Runtime:removeEventListener(“enterFrame”, onEnterFrame)
Runtime:removeEventListener(“touch”, onTouch)
physics.stop()
end
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener(“enterScene”, scene)
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener(“exitScene”, scene)
return scene
[/code] [import]uid: 159438 topic_id: 34189 reply_id: 136029[/import]