Hola,
¿Alguien sabe que acción puedo poner en la colisión de dos objetos para sustituirla por el evento target?
Estoy practicando con la puntuación de un juego que puntúa cuando pulsamos con el dedo sobre el objeto:
Código lua:
Ver originallocal function onTouch( event )
print(event.phase)
if event.phase == "began" then
score.add(event.target.value)
timer.performWithDelay(10, function() event.target:removeSelf(); end, 1)
end
return true
end
...y la quiero cambiar cuando colisiona:
Código lua:
Ver originallocal function onCollision_2(event)
if event.phase == "began" then
local agro = event.object1
local hit = event.object2
if agro.type == "crate" and hit.type == "grass" then
elseif agro.type == "grass" and hit.type == "crate" then
--código para ejecutar la acción de la colisión y añadir puntos
grass.isVisible = false
end
end
end
Runtime:addEventListener("collision", onCollision_2)
Este sería el script completo:
Código lua:
Ver originallocal centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local physics = require( "physics" )
physics.start()
local widget = require( "widget" )
local score = require( "score" )
display.setStatusBar( display.HiddenStatusBar )
local bkg = display.newImageRect( "bkg_clouds.png", 360, 480)
bkg.x = centerX
bkg.y = 240
local grass = display.newImageRect("grass.png", 360, 40)
grass.x = centerX
grass.y = _H - 68
grass.type = "grass"
physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )
local function newCrate()
rand = math.random( 100 )
local crate
if (rand < 60) then
crate = display.newImage("crate.png");
crate.x = 60 + math.random( 160 )
crate.y = -100
physics.addBody( crate, { density=0.9, friction=0.3, bounce=0.3} )
crate.value = 50
crate.type = "crate"
elseif (rand < 80) then
crate = display.newImage("crateB.png");
crate.x = 60 + math.random( 160 )
crate.y = -100
physics.addBody( crate, { density=1.4, friction=0.3, bounce=0.2} )
crate.value = 100
crate.type = "crate"
else
crate = display.newImage("crateC.png");
crate.x = 60 + math.random( 160 )
crate.y = -100
physics.addBody( crate, { density=0.3, friction=0.2, bounce=0.5} )
crate.value = 500
crate.type = "crate"
end
local function onTouch( event )
print(event.phase)
if event.phase == "began" then
score.add(event.target.value)
timer.performWithDelay(10, function() event.target:removeSelf(); end, 1)
end
return true
end
crate:addEventListener( "touch", onTouch )
end
local scoreText = score.init({
fontSize = 20,
font = "Helvetica",
x = display.contentCenterX,
y = 20,
maxDigits = 7,
leadingZeros = true,
filename = "scorefile.txt",
})
local function saveScore( event )
if event.phase == "ended" then
score.save()
end
return true
end
local saveButton = widget.newButton({
width = 200,
height = 64,
x = display.contentCenterX,
y = display.contentHeight - 32,
label = "Save Score",
labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0 } },
fontSize = 32,
onEvent = saveScore
})
local function loadScore( event )
if event.phase == "ended" then
local prevScore = score.load()
if prevScore then
score.set(prevScore)
end
end
return true
end
local saveButton = widget.newButton({
width = 200,
height = 64,
x = display.contentCenterX,
y = display.contentHeight - 64,
label = "Load Score",
labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0 } },
fontSize = 32,
onEvent = loadScore
})
local dropCrates = timer.performWithDelay( 1000, newCrate, 100 )
-------------------------------------------COLISION----------------------------------------
local function onCollision_2(event)
if event.phase == "began" then
local agro = event.object1
local hit = event.object2
if agro.type == "crate" and hit.type == "grass" then
elseif agro.type == "grass" and hit.type == "crate" then
--código para ejecutar la acción de la colisión y añadir puntos
grass.isVisible = false
end
end
end
Runtime:addEventListener("collision", onCollision_2)
Saludosss