Foros del Web » Programación para mayores de 30 ;) » Programación móvil »

[SOLUCIONADO] Cambiar evento touch por otro de collision

Estas en el tema de Cambiar evento touch por otro de collision en el foro de Programación móvil en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 24/11/2014, 13:24
Avatar de Polu  
Fecha de Ingreso: febrero-2003
Mensajes: 135
Antigüedad: 21 años, 2 meses
Puntos: 0
Cambiar evento touch por otro de collision

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 original
  1. local function onTouch( event )
  2.         print(event.phase)
  3.         if event.phase == "began" then
  4.             score.add(event.target.value)
  5.             timer.performWithDelay(10, function() event.target:removeSelf(); end, 1)
  6.         end
  7.         return true
  8.     end
...y la quiero cambiar cuando colisiona:
Código lua:
Ver original
  1. local function onCollision_2(event)
  2.     if event.phase == "began" then
  3.  
  4.         local agro = event.object1
  5.         local hit = event.object2
  6.  
  7.         if agro.type == "crate" and hit.type == "grass" then
  8.  
  9.         elseif agro.type == "grass" and hit.type == "crate" then
  10.        
  11.         --código para ejecutar la acción de la colisión y añadir puntos
  12.  
  13.             grass.isVisible = false
  14.            
  15.            
  16.  
  17.         end
  18.     end
  19. end
  20.  
  21.  
  22.     Runtime:addEventListener("collision", onCollision_2)
Este sería el script completo:
Código lua:
Ver original
  1. local centerX = display.contentCenterX
  2. local centerY = display.contentCenterY
  3. local _W = display.contentWidth
  4. local _H = display.contentHeight
  5.  
  6. local physics = require( "physics" )
  7. physics.start()
  8.  
  9. local widget = require( "widget" )
  10. local score = require( "score" )
  11.  
  12. display.setStatusBar( display.HiddenStatusBar )
  13.  
  14. local bkg = display.newImageRect( "bkg_clouds.png", 360, 480)
  15. bkg.x = centerX
  16. bkg.y = 240
  17.  
  18. local grass = display.newImageRect("grass.png", 360, 40)
  19. grass.x = centerX
  20. grass.y = _H - 68
  21. grass.type = "grass"
  22.  
  23. physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )
  24.  
  25.  
  26. local function newCrate()  
  27.     rand = math.random( 100 )
  28.  
  29.     local crate
  30.  
  31.     if (rand < 60) then
  32.         crate = display.newImage("crate.png");
  33.         crate.x = 60 + math.random( 160 )
  34.         crate.y = -100
  35.         physics.addBody( crate, { density=0.9, friction=0.3, bounce=0.3} )
  36.         crate.value = 50
  37.         crate.type = "crate"
  38.     elseif (rand < 80) then
  39.         crate = display.newImage("crateB.png");
  40.         crate.x = 60 + math.random( 160 )
  41.         crate.y = -100
  42.         physics.addBody( crate, { density=1.4, friction=0.3, bounce=0.2} )
  43.         crate.value = 100
  44.         crate.type = "crate"
  45.     else
  46.         crate = display.newImage("crateC.png");
  47.         crate.x = 60 + math.random( 160 )
  48.         crate.y = -100
  49.         physics.addBody( crate, { density=0.3, friction=0.2, bounce=0.5} )
  50.         crate.value = 500
  51.         crate.type = "crate"
  52.     end
  53.  
  54.     local function onTouch( event )
  55.         print(event.phase)
  56.         if event.phase == "began" then
  57.             score.add(event.target.value)
  58.             timer.performWithDelay(10, function() event.target:removeSelf(); end, 1)
  59.         end
  60.         return true
  61.     end
  62.     crate:addEventListener( "touch", onTouch )
  63. end
  64.  
  65. local scoreText = score.init({
  66.     fontSize = 20,
  67.     font = "Helvetica",
  68.     x = display.contentCenterX,
  69.     y = 20,
  70.     maxDigits = 7,
  71.     leadingZeros = true,
  72.     filename = "scorefile.txt",
  73.     })
  74.  
  75. local function saveScore( event )
  76.     if event.phase == "ended" then
  77.         score.save()
  78.     end
  79.     return true
  80. end
  81.  
  82. local saveButton = widget.newButton({
  83.         width = 200,
  84.         height = 64,
  85.         x = display.contentCenterX,
  86.         y = display.contentHeight - 32,
  87.         label = "Save Score",
  88.         labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0 } },
  89.         fontSize = 32,
  90.         onEvent = saveScore
  91.     })
  92.  
  93. local function loadScore( event )
  94.     if event.phase == "ended" then
  95.         local prevScore = score.load()
  96.         if prevScore then
  97.             score.set(prevScore)
  98.         end
  99.     end
  100.     return true
  101. end
  102.  
  103. local saveButton = widget.newButton({
  104.         width = 200,
  105.         height = 64,
  106.         x = display.contentCenterX,
  107.         y = display.contentHeight - 64,
  108.         label = "Load Score",
  109.         labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0 } },
  110.         fontSize = 32,
  111.         onEvent = loadScore
  112.     })
  113.    
  114. local dropCrates = timer.performWithDelay( 1000, newCrate, 100 )
  115.  
  116. -------------------------------------------COLISION----------------------------------------
  117.  
  118. local function onCollision_2(event)
  119.     if event.phase == "began" then
  120.  
  121.         local agro = event.object1
  122.         local hit = event.object2
  123.  
  124.         if agro.type == "crate" and hit.type == "grass" then
  125.  
  126.         elseif agro.type == "grass" and hit.type == "crate" then
  127.        
  128.         --código para ejecutar la acción de la colisión y añadir puntos
  129.  
  130.             grass.isVisible = false
  131.            
  132.            
  133.  
  134.         end
  135.     end
  136. end
  137.  
  138.  
  139.     Runtime:addEventListener("collision", onCollision_2)
Saludosss
  #2 (permalink)  
Antiguo 30/11/2014, 13:04
Avatar de Polu  
Fecha de Ingreso: febrero-2003
Mensajes: 135
Antigüedad: 21 años, 2 meses
Puntos: 0
Respuesta: Cambiar evento touch por otro de collision

Hola de nuevo, me responderé a mi mismo.

Machacando y machacando al final he dado con la solución, dejo el script por si a alguien le interesa:
Código C:
Ver original
  1. local function onCollision_2(event)
  2.     if event.phase == "began" then
  3.  
  4.         local agro = event.object1
  5.         local hit = event.object2
  6.  
  7.         if agro.type == "crate" and hit.type == "grass" then
  8.  
  9.         elseif agro.type == "grass" and hit.type == "crate" then
  10.        
  11.  
  12.        score.add(500)-------CODE PARA LA PUNTUACIÓN------------
  13.  
  14.  
  15.         end
  16.     end
  17. end
  18.  
  19.  
  20.     Runtime:addEventListener("collision", onCollision_2)

Bye:

Etiquetas: evento, touch
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:21.