I’m having trouble creating the score.
when the little man goes over an animal the score increases by 1, only that he increases it by 2 and after going over an animal he cannot go above the others but instead of running forward he goes back
the code is:
–
– game.lua
– caricamento composer library
local composer = require( “composer” )
– definizione di una nuova scena
local scene = composer.newScene()
– caricamento e inizializzazione fisica
local physics = require(“physics”)
–partenza del sistema fisico
physics.start()
–inizializzazione variabili locali
local music --variabile per impostazione musica
local playSound --variabile per impostazione suono
local bosco1 --immagine di sfondo1
local bosco2 --immagine di sfondo2
local terreno1 --terreno 1
local terreno2 --terreno2
local opt --variabile per impostazione opzioni
local boySheet --variabile impostazione sheet dell’oggetto boy
local runningBoy --variabile per far correre l’oggetto boy
local boy --variabile per oggetto boy
local score=0 --variabile per impostazione punteggio
local highscoreTx --variabile per il testo del punteggio
local obstacle --variabile per la creazione degli ostacoli
local animal --variabile per la creazione degli animali
local sceneGroup --variabile per inserimento oggetti nella scena
local element --variabile per la creazione di nuovi elementi
local elementSpeed=3
local gameLoopTimer
local speed=2 --velocità sfondo e terreno
–tabella per inserimenti di ostacoli e animali
local elements={}
– funzione per far saltare boy quando viene toccato
local function boyJump(self, event)
–se boy non sta saltando…
if boy.action ~= “jump” then
–da commentare
boy:applyLinearImpulse(0,-220,boy.x,boy.y)
boy.action = “run”
end
end
local function onCollision(self,event)
– self rappresenta l’oggetto badGuy
– event.other rappresenta l’altro ogetto
– coinvolto nell’urto
if event.phase==“began” then
if event.other.name==“ground” and event.contact.isTouching==true then
self.action=“run”
end
end
return true
end
– IMPLEMENTAZIONEMPLEMENTAZIONE DELLO SCORRIMENTO ORIZZONTALE DEL TERRENO E DELLO SFONDO
– mentre l’oggetto boy continua a stare nella stessa posizione
function scrolling(self,event)
– self è l’oggetto da muovere verso sinistra
if self.x<-(display.contentCenterX-speed*2) then
self.x = display.contentWidth+display.contentCenterX
else
self.x =self.x - speed
end
return true
end
– Funzione per la creazione degli ostacoli e animali
local function createElement( event )
-- 1, 3, 6, 8 = ostacoli
-- 2, 4, 5, 7 = animali
-- In base al numero cambia l'immagine caricata
local rType = math.random(1, 8)
-- Ostacolo fiamma
if(rType == 1 or rType == 6) then
element = display.newImageRect(sceneGroup,"img/flame.png", 80, 80);
element.myName = "flame"
--element.touchable = true
element.anchorX = 0
element.anchorY = 0
-- L'elemento generato viene posizionato in modo casuale, ma entro i margini dello schermo
element.x = 1080
element.y = 200;
-- Aggiungiamo un corpo statico alla fiamma
physics.addBody(element, "dynamic", { bounce = 0.0 })
-- Ostacolo roccia
elseif(rType == 3 or rType == 8) then
element = display.newImageRect(sceneGroup,"img/rock.png", 70, 50);
element.myName = "roccia"
--element.touchable = true
element.anchorX = 0
element.anchorY = 0
element.x = 1080
element.y = 200;
-- Aggiungiamo un corpo statico alla roccia
physics.addBody(element, "dynamic", { bounce = 0.0 })
-- Animale
elseif (rType == 2 or rType == 5) then
element = display.newImageRect(sceneGroup,"img/rabbit.png", 80, 80);
element.myName = "rabbit"
element.anchorX = 0
element.anchorY = 0
element.x = 1080
element.y = 200;
-- Aggiungiamo un corpo statico al coniglio
physics.addBody(element, "dynamic", { bounce = 0.0 })
else
element = display.newImageRect(sceneGroup,"img/koala.png", 200, 120);
element.myName = "koala"
element.anchorX = 0
element.anchorY = 0
-- L'elemento generato viene posizionato in modo casuale, ma entro i margini dello schermo
element.x = 1080
element.y = 150;
-- Aggiungiamo un corpo statico al koala
physics.addBody(element, "dynamic", { bounce = 0.0 })
end
table.insert(elements, element)
return element
end
–funzione che implementa lo scorrimento orizzontale di ogni ostacolo attivo
local function obstacleScroll(self, event)
self.x=self.x - elementSpeed
end
– funzione che elimina gli ostacoli non attivi e ne crea di nuovi
– e viene chiamato ogni secondo
–funzione per gestire il loop del videogioco
local function gameLoop()
– creazione nuovo ostacolo fuori dal display
element=createElement()
– attivazione dello scorrimento orizzontale degli ostacoli
– per farli entrare nel display
element.enterFrame = obstacleScroll
Runtime:addEventListener(“enterFrame”,element)
-- analizza la tabella degli ostacoli attivi alla ricerca di
-- ostacoli attivi che si trovano nel margine sinistro del display
-- gli ostacoli e gli ascoltatori vengono rimossi insieme
-- all'entrata degli ostacoli nella tabella
for i,thisElement in ipairs(elements) do
if thisElement.x < -display.contentWidth then
Runtime:removeEventListener("enterFrame",thisElement)
display.remove(thisElement)
table.remove(elements,i)
end
end
end
–variabile che contiene il timer del loop del gioco
–viene richiamato gameLoop ogni secondo per mezzo di timer.performWithDelay
–2500 è il ritardo con cui vengono creati gli elementi
gameLoopTimer = timer.performWithDelay(2500,gameLoop,0)
–funzione che viene richiamata quando l’oggetto boy collide con un ostacolo
local function elementCollision(self,event)
if (event.phase==“began”) then
–se la collisione avviene tra boy, la fiamma e la roccia
if ((event.other.myName==“flame” and self.myName==“boy”) or (event.other.myName==“roccia” and self.myName==“boy”)) then
physics.pause()
timer.cancel(gameLoopTimer)
display.remove(boy)
boy = nil
–rimozione suono in sottofondo
audio.stop(playSound)
playSound=nil
–liberazione memoria
audio.dispose(playSound)
Runtime:removeEventListener(“enterFrame”, bosco1)
Runtime:removeEventListener(“enterFrame”, bosco2)
Runtime:removeEventListener(“enterFrame”, terreno1)
Runtime:removeEventListener(“enterFrame”, terreno2)
Runtime:removeEventListener(“enterFrame”, onCollision)
Runtime:removeEventListener(“enterFrame”, elementCollision)
Runtime:removeEventListener(“enterFrame”, element)
for i=#elements,1,-1 do
local thisElement=elements[i]
Runtime:removeEventListener("enterFrame",thisElements)
end
composer.gotoScene(“restart”,{ time=800, effect=“crossFade” } )
end
end
if ((event.other.myName==“rabbit” and self.myName==“boy”) or (event.other.myName==“koala” and self.myName==“boy”)) then
if (event.contact.isTouching==true)then
score=score + 1
end
event.contact.isTouching=false
–l’animale “salvato” scompare
transition.fadeOut(event.other, {time=60})
print(“score”)
print(score)
highscoreTx.text=“Score” … score
end
end
– creazione scene ()
function scene:create( event )
sceneGroup = self.view
– Code here runs when the scene is first created but has not yet appeared on screen
–caricamento di un suono in sottofondo
music=audio.loadStream(“audio/Sand_Castle.mp3”)
–riproduzione suono con loop continuo
playSound=audio.play(music, {loops=-1})
–impostazione background
bosco1 = display.newImageRect(sceneGroup, “img/back1.jpg”,display.contentWidth,display.contentHeight)
bosco1.x=display.contentCenterX
bosco1.y = display.contentCenterY
bosco2 = display.newImageRect(sceneGroup, “img/back2.jpg”,display.contentWidth,display.contentHeight)
bosco2.x=display.contentCenterX+display.contentWidth
bosco2.y = display.contentCenterY
– impostazione terreno
terreno1 = display.newImageRect(sceneGroup, “img/ground.png”,1920,105)
terreno1.x = display.contentCenterX
terreno1.y = display.contentHeight-52
terreno1.myName=“ground”
terreno2 = display.newImageRect(sceneGroup, “img/ground.png”,1920,105)
terreno2.x = display.contentCenterX+display.contentWidth
terreno2.y = display.contentHeight-52
terreno2.myName=“ground”
–impostazione boy
opt={width =93, height= 150, numFrames = 60}
boySheet=graphics.newImageSheet(“img/boy.png”,opt)
runningBoy={
{
name = "runRight",
start = 31,
count = 30,
time = 300,
loopCount = 0,
loopDirection ="forward"
}
}
boy=display.newSprite(boySheet, runningBoy)
boy.x=50
boy.y=display.contentHeight-232
boy:setSequence(“runRight”)
boy:play()
boy.action=“run”
boy.myName=“boy”
– aggiunta corpo fisico dinamico a boy, con attrito elevato e rimbalzo 0
physics.addBody(boy,“dynamic”,{friction=1.0,bounce=0.0,density=1.3})
boy.isFixedRotation = true
– INIZIO GENERAZIONE MONDO FISICO
– trasformazione terreno1 e terreno2
– in corpi fisici statici con elevato attrito e nessun rimbalzo
physics.addBody(terreno1,“static”,{friction=1.0,bounce=0.0,density=1.8})
physics.addBody(terreno2,“static”,{friction=1.0,bounce=0.0,density=1.8})
–scorrimento orizzontale sfondo e terreno
bosco1.enterFrame = scrolling
Runtime:addEventListener( “enterFrame”, bosco1)
bosco2.enterFrame = scrolling
Runtime:addEventListener( “enterFrame”, bosco2 )
terreno1.enterFrame=scrolling
Runtime:addEventListener(“enterFrame”,terreno1)
terreno2.enterFrame=scrolling
Runtime:addEventListener(“enterFrame”,terreno2)
– FINE IMPLEMENTAZIONE SCORRIMENTO ORIZZONTALE
–listener di tocco su boy
–quando boy viene toccato salta
boy:addEventListener(“tap”,boyJump)
– attivare il listener di tocco su boy
boy.collision = onCollision
boy:addEventListener(“collision”,boy)
–associa a boy un ascoltatore locale per l’evento collision
–l’ascoltatore deve essere un table listener che richiama la funzione
–obstacleCollision
boy.collision=elementCollision
boy:addEventListener(“collision”, boy )
end
– show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
--impostazione testo per il punteggio
highscoreTx=display.newText(sceneGroup, "Score:", display.contentCenterX, display.contentCenterY-150, native.systemFont, 30)
highscoreTx:setFillColor(black)
elseif ( phase == "did" ) then
– Code here runs when the scene is entirely on screen
end
end
scene:addEventListener(“show”)
– hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
physics.pause()
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
– destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
--eliminazone scena restart
composer.removeScene("restart")
end
– Scene event function listeners
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
what is the problem? thanks!