hy…i’m kind of new to the corona (about a week since i started), and actually i have manage so far creating about 75% of a game (i still need to add some features).
the problem i’m getting is related to the collisions of two objects.
so far the whole system is based on gravity…and i have an object that i move with a touch event…(player.x=event.x - player is a kinematic object so it will not be affected by gravity… there are three more object types involved in this game…enemy,shield and bonus (you could imagine what i`m trying to achieve when a collision happens…)
the problems i have regarding this collision is that it appears some time two collisions on the same objects event though i delete the objects (on the second i get an error because the object colliding with player is already deleted) i even tried to pause the physics engine when the collision event is triggered and to restart it after i delete the objects…same thing happends…here is some of the code i have:
local function playerMovement(event)
if not gameIsActive then return false end
if event.x >= halfPlayerWidth and event.x <= display.contentWidth - halfPlayerWidth then
player.x = event.x
–here i even tried to make it with animate:
–transition.to(player,{time=(100*(math.abs(player.x-event.x)))/display.contentWidth,x=event.x})
–and yes i try to calculate the time so every time player moves with the same speed
end
end
player:addEventListener(“touch”, playerMovement)
local function onCollision(self, event)
if self.name==“player” and event.other.name == “enemy” and gameIsActive then
life = life - 1
lifeText.text = life
–maybe this will work
physics.pause()
table.insert(toRemove3, event.other)
end
—almost same if for the rest of the collision cases player-bonus, shield-enemy, shield-bonus
–shield is bigger then player and positioned in the same place as player,
…
–in the game loop
for i = 1, #toRemove2 do
–for collision betwin player and bonus or shield with bonus
local rand=math.random(1,10)
if rand == 1 then
–10 percent chances you get a shield
if shield==nil then
shield=display.newImage(“Graph/shield.png”);
shield.x=player.x
shield.y=player.y
physics.addBody(shield,{bounce = 0.2, radius = shield.contentHeight/2} )
addshield=physics.newJoint(“weld”,shield,player,player.x-15,player.y)
shield.name=“shield”
shield.collision = onCollision
shield:addEventListener(“collision”, shield)
playerLayer:insert(shield)
end
end
if rand==2 then
–same as shield for extra life
life=life+1
lifeText.text=life
end
–and here is where i get the error when the second collision appears, because it the bonus is allready removed…and this is not happening all the time. on android devices i get this more often the Ios
toRemove2[i].parent:remove(toRemove2[i])
toRemove2[i] = nil
–like i said i tried to pause a little the physics now i get this a little less then before i paused the engine on the collision event
physics.start()
end
hope somebody will figure it out if i make some stupid things around here…or can help me figure out what to do to stop this from happening (Again…this only happens when the player is moving)
Thank you in advance [import]uid: 57170 topic_id: 9972 reply_id: 309972[/import]