playing sprite Instance at the move phase of touch event - coronasdk

I want to move frames of sprite sheet with the drag event.The sprite Instance must play only when the user drags .If he lifts his hand it must get paused.Is there any way to do this?

require "sprite"
-- A sprite sheet with a green dude
local sheet = sprite.newSpriteSheet( "greenman.png", 128, 128 )
local spriteSet = sprite.newSpriteSet(sheet, 1, 15)
sprite.add( spriteSet, "man", 1, 15, 200, 0 ) -- play 15 frames every 200 ms
local instance = sprite.newSprite( spriteSet )
instance.x = display.contentWidth / 2
instance.y = display.contentHeight / 2
instance:prepare("man")
local function startDrag( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
t:play()
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
t:pause()
end
end
-- Stop further propagation of touch event!
return true
end
instance:addEventListener( "touch", startDrag )

Related

Highlight a path in a grid

In my grid-based game, The player clicks on a unit then he moves his finger to determine where this unit should move. I'm using the "Jumper" library for the pathfinding. The code for getting the path works perfectly, but the code for highlighting the path, not so much.
local function onTileTouch( event )
local phase = event.phase
local tile = event.target
if ( phase == "began" ) then
-- I could create the line here
elseif ( phase == "moved" ) then
createPath( tile )
-- Getting the position of the first tile based on where the unit is
local t = tiles[currentSelectedUnit.pos.y][currentSelectedUnit.pos.x]
-- Create the line at the first tile's position
line = display.newLine( t.x, t.y, t.x, t.y )
line:setStrokeColor( 1,0,0 )
line.strokeWidth = 8
-- "foundPath" is a table of tiles of the correct path
for i=1,#foundPath do
line:append( foundPath[i].x,foundPath[i].y )
end
elseif ( phase == "ended" or phase == "cancelled" ) then
end
The line doesn't look right when being created in the "moved" phase. It does, however, look very accurate when being created in the "began" phase and then getting appended during the "moved" phase. But in this case, another extra line gets drawn that doesn't follow the path but gets directly from the start tile to the end tile.
My second problem with the "began" phase method, is I don't know how to keep deleting the old line and create a new one with for the new correct path.
Let me know if any extra information is needed.
I'm not sure what exactly you want so I have prepared my own version:
test.lua
local composer = require( 'composer' )
-- Library setup
local Grid = require ( 'jumper.grid' ) -- The grid class
local Pathfinder = require ( 'jumper.pathfinder' ) -- The pathfinder lass
local scene = composer.newScene()
-- First, set a collision map and value for walkable tiles
local board = {
tiles = {},
walkable = 0,
map = {
{0,1,0,0,0},
{0,1,0,1,0},
{0,1,0,0,0},
{0,0,0,0,0},
}
}
local function createPath( startx, starty, endx, endy )
-- Creates a grid object
local grid = Grid( board.map )
-- Creates a pathfinder object using Jump Point Search
local myFinder = Pathfinder( grid, 'JPS', board.walkable )
-- Calculates the path, and its length
local path = myFinder:getPath( startx, starty, endx, endy )
return path
end
function scene:create( event )
local sceneGroup = self.view
local rowNum = #board.map
local colNum = #board.map[1]
local width = 40
local height = width
local margin = 5
local function touch( self, event )
local phase = event.phase
local endTile = event.target
if ( phase == 'began' ) then
board.startTile = endTile
elseif ( phase == 'moved' ) then
if board.startTile ~= endTile then
if ( board.endTile ~= endTile ) then
board.endTile = endTile
local path = createPath( board.startTile.col, board.startTile.row, board.endTile.col, board.endTile.row )
if path then
-- Remove old lines
display.remove( board.lines )
board.lines = nil
-- Create new line
board.lines = display.newLine( sceneGroup, board.startTile.x, board.startTile.y, board.startTile.x, board.startTile.y + 1 )
for node, count in path:nodes() do
local id = colNum * ( node:getY() - 1 ) + node:getX()
local tile = board.tiles[id]
board.lines:append( tile.x, tile.y )
end
end
end
else
-- Remove old lines
display.remove( board.lines )
board.lines = nil
board.endTile = nil
end
elseif ( phase == 'ended' or phase == 'cancelled' ) then
-- Remove old lines
display.remove( board.lines )
board.lines = nil
board.endTile = nil
board.startTile = nil
end
end
for i=1, rowNum do
for j=1, colNum do
local tile = display.newRect( sceneGroup, (width + margin ) * j, ( height + margin ) * i, width, height )
tile.col = j
tile.row = i
tile.touch = touch
-- Add listener and change color for walkable tile
if board.map[i][j] == board.walkable then
tile:addEventListener( 'touch' )
tile:setFillColor( 0, 0.5, 0.5 )
end
local id = colNum * ( i -1 ) + j
board.tiles[id] = tile
end
end
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == 'will' then
elseif phase == 'did' then
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == 'will' then
elseif phase == 'did' then
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( 'create', scene )
scene:addEventListener( 'show', scene )
scene:addEventListener( 'hide', scene )
scene:addEventListener( 'destroy', scene )
return scene

Dragging object in corona

I have an object that I just wanted to move up and down. How could I make that object still move up and down without actually touching the object? Here is my codes so far:
function Scientist:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( nil)
self.markY = self.y
self.isFocus = false
elseif event.phase == "moved" then
local y = (event.y - event.yStart) + self.markY
self.y = y
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
Scientist:addEventListener('touch', Scientist)
Just create a large rectangle, set the alpha to 0, and make the width display.contentWidth and the height display.contentHeight. When you touch that rectangle, have that move the Scientist in a similar way you are doing now.
Make a rectangle on entire page with alpha set to 0.01:
local background = display.newRect(display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight)
then write a touch listener like you did background:addEventListener('touch', background)
here is the complete code for you:
function Scientist:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( nil)
Scientist.markY = Scientist.y
self.isFocus = false
elseif event.phase == "moved" then
local y = (event.y - event.yStart) + Scientist.markY
Scientist.y = y
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
Scientist:addEventListener('touch', Scientist)

Move a object by flicking

I can move the box by touching it and dragging it along the x-axis, but i like to be able to flick it from one side to the other. is there a simple solution to do this?
local box = display.newRect( 0, 0, 50, 50)
box:setFillColor( math.random(0,255), math.random(0,255), math.random(0,255) )
physics.addBody( box, { density=3.0, friction=0.5 } )
box.gravityScale = 0.0
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
self.x = x
end
return true
end
box:addEventListener( "touch", box )
You could use transition.to, but you'll have to decide by how much to move. I'll assume you want to move it all the way to other side:
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
elseif event.phase == "end" then
local targetX = 0 -- if flick left
if event.x > self.markX then -- flick right
targetX = display.contentWidth - self.width
end
local duration = 1000 -- 1 second
transition.to(self, {duration, x=targetX})
end
return true
end
Have not tested, could be syntax or other errors, but this should give you a good idea of how to proceed (if not put a comment).

Game speed increases itself on every restart - Corona

am trying to make a jetpack joyride concept game on corona. First of all unlike other sdk where when you change a scene, the previous scene is automatically removed, in corona you have to remove each and object and function yourself.
After a week I was finally able to remove objects while changing scene, but now I have ran into a new problem. Everytime when I change scenes and come back to the gameScreen the speed of lasers and missiles increases every time ( maybe twice? ). How can I reset its speed?
I even added another "gameStatus" variable to remove the event listener and tried to print it, but game status never actually returned "ended", it was always "running". Here is the gameScene screen where everything happens
EDIT : I just noticed something, For example if 3 objects have already spawned then after reloading the screen only first 3 objects seem to be fast and its normal from fourth.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
local image, text1
local function onSceneTouch( self, event )
if event.phase == "began" then
composer.gotoScene( "startScreen", "fade", 400 )
return true
end
end
function scene:create( event )
local sceneGroup = self.view
-- Variable
---------------------------------------
sW = display.contentWidth
sH = display.contentHeight
-- trying to reset gameStatus on restart
gameStatus = "ended"
gameStatus = "running"
--------------------------------------------------------
backgroundTiles = {}
drawBackground = function()
cieling = display.newRect( 0, 0, 2 * sW, sH * 0.05)
physics.addBody( cieling, "static", { density=0, friction=0, bounce=0 } )
cieling.x, cieling.y = cieling.contentWidth / 4, cieling.contentHeight * 0.25
cieling.alpha = 0
cieling.name = "ceiling"
ground = display.newRect( 0, sH, 2 * sW, sH * 0.05)
physics.addBody( ground, "static", { density=3, friction=1, bounce=0.1 } )
ground.x, ground.y = ground.contentWidth / 4, sH - ground.contentHeight * 0.25
ground.alpha = 0
ground.name = "ground"
end
drawBackground()
--------------------------------------------------------
--------------------------------------------------------
drawBird = function()
bird = display.newImageRect( "a/img/hero/hero.png", 80, 58)
bird.x, bird.y = 0 - sW * 0.1, ground.y - ground.contentHeight - 10
bird.isFixedRotation = true; bird.angularVelocity = 0;
physics.addBody( bird, { density=1, friction=0.5, bounce=0.1 } )
bird.name = "bird"
sceneGroup:insert( bird )
bird:addEventListener( "collision", onCollision )
end
coinsCollected, tokensCollected = 0, 0
birdFlight = function()
if ( gameStatus == "running" ) then
transition.to(bird, {
y = bird.y - 75,
transition = easing.outQuad,
onComplete = function() end
})
function birdFlight()
--transition.to( bird, { rotation=-45, time=300 } )
end
birdFlight()
function birdFall()
--transition.to( bird, { rotation=90, time=300 } )
end
timer.performWithDelay(700, birdFall, 1)
end
end
display.currentStage:addEventListener( "tap", birdFlight )
function onCollision(event)
if event.phase == "began" then
if event.other.name == "coin" then
coinsCollected = coinsCollected + 1
end
if event.other.name == "token" then
tokensCollected = tokensCollected + 1
end
if event.other.name == "missile" or event.other.name == "laser" or event.other.name == "rod" then
-- game ended
end
end
end
drawBird()
function atFrame(event)
if gameStatus == "running" then
bird.x,bird.y = sW / 3,bird.y + 9
if ( bird.y > sH - 70) then bird.y = sH - 70 end
if ( gameStatus == "ended" ) then bird.y = bird.y + 15 end
end
end
Runtime:addEventListener( "enterFrame", atFrame )
--------------------------------------------------------
--------------------------------------------------------
missile, missileAlert, missileMoving = {}, {}, {}
function removeMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and gameStatus == "running" then
if missile[i].x < -100 then
display.remove( missile[i] )
missile[i] = nil
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function flyMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and missile[i].y ~= nil and gameStatus == "running" then
if missileMoving[i] == true then
missile[i].y = bird.y
end
missile[i].x = missile[i].x - 5
if missileAlert[i] ~= nil then
if missileMoving[i] == true then
missileAlert[i].y = bird.y
end
if missile[i].x < sW then
display.remove( missileAlert[i] )
missileAlert[i] = nil
end
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function holdMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and gameStatus == "running" then
if missile[i].x < sW * 1.5 then
if missileAlert[i] ~= nil then
missileAlert[i]:setFillColor(1,1,0)
end
missileMoving[i] = false
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function spawnMissile(i)
missile[i] = display.newRect( sW*2, sH/2, 80, 80 )
missileAlert[i] = display.newRect( sW-80, bird.y, 80, 80 )
physics.addBody(missile[i],"kinematic",{isSensor=true})
missile[i].name = "missile"
sceneGroup:insert( missile[i] )
sceneGroup:insert( missileAlert[i] )
missileMoving[i] = true
flyMissile(i)
removeMissile(i)
holdMissile(i)
end
spawnMissile(1)
--------------------------------------------------------
--------------------------------------------------------
laser = {}
function moveAndRemovelaser(i)
local onEnterFrame = function( event )
if laser[i] ~= nil and laser[i].x ~= nil and gameStatus == "running" then
laser[i].x = laser[i].x - 5
if laser[i].x < 0 - sW / 2 then
display.remove( laser[i] )
laser[i] = nil
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function spawnlaser(i)
laserSize = math.random(1,2)
laserPosition = math.random(1,3)
laserRotation = math.random(1,4)
if laserSize == 1 then
laser[i] = display.newRect( 100,100,50,sH/3 )
else
laser[i] = display.newRect( 100,100,50,sH/2 )
end
sceneGroup:insert( laser[i] )
laser[i].x = sW * 2
laser[i].y = sH / 2
laser[i].name = "laser"
if laserPosition == 1 and laserRotation ~= 4 then
laser[i].y = sH * 0.05 + 12
laser[i].anchorY = 0
elseif laserPosition == 3 and laserRotation ~= 4 then
laser[i].y = sH * 0.95 - 12
laser[i].anchorY = 1
end
if laserPosition == 1 and laserRotation == 4 then
laser[i].y = sH * 0.05 + laser[i].contentHeight / 2
elseif laserPosition == 3 and laserRotation == 4 then
laser[i].y = sH * 0.95 - laser[i].contentHeight / 2
end
if laserRotation == 1 then
laser[i].rotation = -45
elseif laserRotation == 2 then
laser[i].rotation = 0
elseif laserRotation == 3 then
laser[i].rotation = 45
elseif laserRotation == 4 then
local onEnterFrame = function( event )
if laser[i] ~= nil and laser[i].rotation ~= nil then
laser[i].rotation = laser[i].rotation + 5
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
end
laser[i]:setFillColor(1,1,0)
physics.addBody(laser[i],"kinematic",{isSensor=true})
moveAndRemovelaser(i)
end
spawnlaser(1)
--------------------------------------------------------
image = display.newRect (100,100,100,100)
image.x = display.contentCenterX
image.y = display.contentCenterY
sceneGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( "Game Screen", 0, 0, native.systemFontBold, 24 )
text1:setFillColor( 255 )
text1.x, text1.y = display.contentWidth * 0.5, 50
sceneGroup:insert( text1 )
end
function scene:show( event )
local phase = event.phase
if "will" == phase then
gameStatus = "ended"
end
if "did" == phase then
print( "4" )
composer.removeScene( "startScreen" )
image:addEventListener( "touch", image )
gameStatus = "running"
end
end
function scene:hide( event )
local phase = event.phase
if "will" == phase then
print( "5" )
gameStatus = "ended"
image:removeEventListener( "touch", image )
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function scene:destroy( event )
print( "6" )
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
You are getting this problem because you are registering an event listener twice.
You need to add and remove them only when the scene enters and exits. The proper place to remove game objects is scene:destroy() and scene:hide()
My advice is that you try to re-organize your code and try to follow these guidelines:
scene:create() : This happens the first time a scene is used. You want create your game objects here. This event will only happen once, unless the scene is destroyed and then used again.
scene:show() : You want to register all your event listeners here (touch, etc) and setup your game. This function gets called every-time the scene is shown. Every event listener that is registered here should be un-registered to prevent double-triggering.
scene:hide() : You want to un-register all event listeners here. This will prevent the next scene from triggering events on your previous scene's listeners too.
scene:destroy() : You want to remove all game objects here (images, text, widgets, etc). The scene is no longer needed and everything should be cleaned.
To better understand how the Scene's work I recommend reading this doc:
http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/
So I ended up creating a function and putting all the removeEventListeners in that function and called that function when the game ended.
For future reference always keep a list of all the timer and event listeners that you have created anywhere in the screen and remove them all together when ending the game

Saving values to another document in Lua with Corona SDK

I'm trying to save highscores to a table whenever a gameOver function is called.
When I go back in the app and try to read the highscores(They're displayed as newText where text is set to the proper highscore of the level.).
But it's not doing what it's supposed to. I can read the the levels highscores but when I try to change the values in the table, the highscore text doesn't change.
I have my main.lua and a myData.lua - The highscore table should be placed in the myData.lua in order to reach it from all the levels in the game.
this is my table
highScores = {
1,
2,
3,
4,
5,
6,
7
}
and I was trying to save/change the value with
highScores[1] = score
Where score is the score count in-game.
I've realized this is not the way to go about it, and what I can find on google seems to overly complicated for what I see as a simple task.
What am I doing wrong?
This is my entire level1.lua - The actual level that's running and trying to save it's score to the highScore table level1.lua):
local composer = require( "composer" )
local scene = composer.newScene()
local myData = require( "myData" )
local physics = require("physics")
physics.setDrawMode( "hybrid" )
-- forward references
local w = display.actualContentWidth
local h = display.actualContentHeight
local dropCount = 0
local spawnShit = 0
local allowDrop = 1
local spawnTime = 17
local countdownTimer
local score
local gX = 0
local gY = 0
score = 0
local countDownNumber = 10
local gameOver
local scoreT = display.newText( {text="Score: "..score, font=system.nativeSystemFont, fontSize=14,} )
scoreT.x = w * 0.5
scoreT.y = h * 0.1
local countDownText = display.newText( {text="", font=system.nativeSystemFont, fontSize=14} )
countDownText.x = w * 0.5
countDownText.y = h * 0.2
local drop01 = display.newImage("drop01.png")
drop01.x = -100
local drop02 = display.newImage("drop02.png")
drop02.x = -100
local drop03 = display.newImage("drop03.png")
drop03.x = -100
local drop04 = display.newImage("drop04.png")
drop04.x = -100
local timerSpawn
local timer2
-- Display objects
local background = display.newImage( "bluebg.png" )
background.x = w*0.5
background.y = h*0.5
background.width = w
background.height = h
local bckBtn = display.newText({text="<--BACK", font=system.nativeSystemFont, fontSize=14})
bckBtn.x = 50
bckBtn.y = 20
local egon = display.newImage( "Egon.png" )
egon.x = w*0.5
egon.y = h*0.85
egon.width = 100
egon.height = 97
local destroyAll = display.newRect( 0, h, w, 10 )
destroyAll.width = w*2
destroyAll.alpha = 0
local overlayBg = display.newRect( -500, -500, w, h )
overlayBg:setFillColor( 0, 0, 0 )
overlayBg.alpha = 0.4
--functions
function gameOver ()
if timerSpawn == nil then
else
timer.cancel(timerSpawn)
timerSpawn = nil
spawnShit = 0
end
if countdownTimer == nil then
else
timer.cancel(countdownTimer)
countdownTimer = nil
end
highScores[1] = score
transition.to( overlayBg, {x=w/2, y=h/2, time=500 } )
end
function goBack (event)
if "began" == event.phase then
gameOver()
if timerSpawn == nil then
else
timer.cancel(timerSpawn)
end
if countdownTimer == nil then
else
timer.cancel(countdownTimer)
end
elseif event.phase == "ended" then
timer2 = timer.performWithDelay(1000, function()
composer.gotoScene("select", "fade", 500)
end)
if overlayBg == nil then
else
overlayBg:removeSelf( )
end
return true
end
return true
end
function moveEgon (event)
if "moved" == event.phase then
egon.x = event.x
end
end
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
function spawnObjects (event)
dropCount = math.random(1,4)
--if stopTimer == 1 then
-- timerSpawn = nil
--spawnShit = nil
--end
if spawnShit == 1 then
print( 'spawnShit' )
if dropCount == 1 then
-- Drop01 function and settings
drop01 = display.newImage( "drop01.png" )
drop01.x = math.random(10, 470)
drop01.y = -40
drop01.width = 50
drop01.height = 50
drop01.myName = "01"
physics.addBody( drop01, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
elseif dropCount == 2 then
--Do shit for drop02
drop02 = display.newImage( "drop02.png" )
drop02.x = math.random(10, 470)
drop02.y = -40
drop02.width = 50
drop02.height = 50
drop02.myName = "02"
physics.addBody( drop02, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
elseif dropCount == 3 then
drop03 = display.newImage( "drop03.png" )
drop03.x = math.random(10, 470)
drop03.y = -40
drop03.width = 50
drop03.height = 50
drop03.myName = "03"
physics.addBody( drop03, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
elseif dropCount == 4 then
drop04 = display.newImage( "drop04.png" )
drop04.x = math.random(10, 470)
drop04.y = -40
drop04.width = 50
drop04.height = 50
drop04.myName = "04"
physics.addBody( drop04, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
end
end
return true
end
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
function onCollision (event)
if "began" == event.phase then
--v--do shit when touching surface
if event.other.myName == "01" then
-- Do shit for drop01 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score+1
countDownNumber = countDownNumber + 10
scoreT.text = "Score: "..score
end
if event.other.myName == "02" then
-- Do shit for drop02 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score+1
scoreT.text = "Score: "..score
end
if event.other.myName == "03" then
-- Do shit for drop03 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score-1
scoreT.text = "Score: "..score
end
if event.other.myName == "04" then
-- Do shit for drop04 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score-1
scoreT.text = "Score: "..score
end
elseif "ended" == event.phase then
-- Do shit when leaving surfaces
end
return true
end
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
function showCountDown (event)
-- Condition to show and hide countdown
if countDownNumber <= 1 or score == -1 then
spawnShit = 0
countDownNumber = 0
timer.cancel(timerSpawn)
timer.cancel(countdownTimer)
countdownTimer = nil
highScores[1] = score
print( 'NO MORE SPAAAAAAAAAAAAAAAWWNS' )
end
if countDownNumber >= 1 then
countDownNumber = countDownNumber -1
countDownText.text = countDownNumber
spawnShit = 1
end
return true
end
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
function destroy (event)
if "began" == event.phase then
event.other:removeSelf( )
else if "ended" == event.phase then
end
return true
end
end
--function scene:create( event )
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc
--Listeners
background:addEventListener( "touch", moveEgon )
bckBtn:addEventListener( "touch", goBack )
egon:addEventListener( "collision", onCollision )
destroyAll:addEventListener( "collision", destroy )
--SceneGroup insert
sceneGroup:insert( background )
sceneGroup:insert(egon)
sceneGroup:insert(bckBtn)
sceneGroup:insert(drop01)
sceneGroup:insert(drop02)
sceneGroup:insert(drop03)
sceneGroup:insert(drop04)
sceneGroup:insert(scoreT)
sceneGroup:insert(countDownText)
sceneGroup:insert(overlayBg)
sceneGroup:insert(destroyAll)
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
physics.start( )
gX = 0
gY = 10
physics.setGravity( gX, gY )
timercount = 10
spawnShit = 1
score = 0
scoreT.text = "Score: "..score
-- ADD physic bodies ----
physics.addBody( egon, "static", {density=0.1, friction=0.1, bounce=0.8 } )
physics.addBody( destroyAll, "static", {density=0.1, friction=0.1, bounce=0.1 } )
countDownNumber = 10
if countdownTimer == nil then
countdownTimer = timer.performWithDelay( 1000, showCountDown, 0 )
else
end
----------- Timers ------------
if timerSpawn == nil then
timerSpawn = timer.performWithDelay(500, spawnObjects, 0 )
else
end
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio,
--timer.pause( timerSpawn )
physics.stop()
spawnShit = nil
score = nil
timerSpawn = nil
countdownTimer = nil
overlayBg = nil
--timer.cancel(timerSpawn)
physics.removeBody( egon )
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
bckBtn:removeEventListener("touch", goBack )
egon:removeEventListener("touch", moveEgon )
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene
Allright. The scores are being saved. The problem was the way i displayed the score text, because it wasn't reloading, it was just re-using the old value.
FIX:
i added an
score01.text = highScore[1]
everytime the scene shows and it automatically updates the score when the select screen shows.

Resources