Hi, I’m trying to make a top view runner game in which the player has to collect coins and avoid enemies. The player character, enemies and coins are physics bodies with functions being called when a collision event occurs involving a coin or an enemy.
(y_enem1 initially set =0.4*height and y_gap=0.05*height)
[lua]physics.start( )
physics.pause( )
for iter=1,num_coins do
coins[iter]=Food:new()
coins[iter]:position(math.random(0.06*width,0.92*width),0-y_gap)
physics.addBody( coins[iter], “dynamic”, {density=0, friction=1, bounce=1, isSensor=true } )
print(coins[iter].x…" "…coins[iter].y)
y_gap=y_gap+(0.1*height)
end
for enem_iter1=1,num_enemy1 do
enemies_1[enem_iter1]=Kasai1:new()
enemies_1[enem_iter1]:position(math.random(0.12*width,0.6*width),(centery-(1.5*y_enem1)))
physics.addBody( enemies_1[enem_iter1], “dynamic”, {shape=enemy1_outline, density=1, bounce=1,isSensor=true} )
print(enemies_1[enem_iter1].x…" "…enemies_1[enem_iter1].y)
y_enem1=y_enem1+(math.random(0.7,1.1)*height)
end
y_enem1=0.6*height
for enem_iter2=1,num_enemy1 do
enemies_2[enem_iter2]=Kasai2:new()
enemies_2[enem_iter2]:position(math.random(0.65*width,0.92*width),(centery-(2*y_enem1)))
physics.addBody( enemies_2[enem_iter2], “dynamic”, {shape=enemy1_outline, density=1, bounce=1,isSensor=true} )
print(enemies_2[enem_iter2].x…" "…enemies_2[enem_iter2].y)
y_enem1=y_enem1+(math.random(0.6,1.5)*height)
end[/lua]
num_coins=15
num_enemy1=8
Thus total of 15 coins and 16 enemies. The code above is used to create patterns of objects and to place them at random yet appropriate gaps.
Furthermore these are the collision handlers:
[lua]function onCoinCollision(self,event)
if(event.phase==“began”) then
–print(“Coin collided”)
local move_coin=function()
y_gap=math.random(0.7,4)*height
self.y=-1*y_gap
self.x=math.random(0.15*width,0.85*width)
end
if(event.other==player) then
num_plants=num_plants+1
text_plants.text="Lusan eaten: "…num_plants
print(num_plants)
end
timer.performWithDelay( 1, move_coin ,1 )
end
end
function onEnemyCollision(self,event)
if(event.phase==“began”) then
print(“Enemy1 collided”)
local move_enemy1=function()
self.y=0-y_enem1
y_enem1=height*(math.random(0.6,3))
self.x=math.random(0.15*width,0.85*width)
end
if (event.other==player) then
lives=lives-1
end
timer.performWithDelay( 1, move_enemy1 ,1 )
end
if(lives<1) then
prepare_end_game()
end
end
for iter=1,num_coins do
coins[iter]:setLinearVelocity( 0, 196 )
coins[iter]:addEventListener( “collision”, coins[iter] )
coins[iter].collision=onCoinCollision
end
for enem_iter1=1,num_enemy1 do
enemies_1[enem_iter1]:setLinearVelocity( 0, 196 )
enemies_1[enem_iter1]:addEventListener( “collision”, enemies_1[enem_iter1] )
enemies_1[enem_iter1].collision=onEnemyCollision
end
for enem_iter2=1,num_enemy1 do
enemies_2[enem_iter2]:setLinearVelocity( 0, 196 )
enemies_2[enem_iter2]:addEventListener( “collision”, enemies_2[enem_iter2] )
enemies_2[enem_iter2].collision=onEnemyCollision
end
physics.start( )[/lua]
In the simulator, my game runs fine, the coins and enemies are placed where I intend them to be placed. Moreover, if enemies or coins happen to be placed next to each other and collide, they get placed somewhere else due to the collision handling.
Unfortunately, when I compiled my game into android build and ran in on my phone ( Huawei G610), all of the coins and enemies continue to appear at the top of the screen, collide with each other and reappear at the top again. I have added a link to a screenshot I took of this occurrence. Look at the top of the picture.
https://app.box.com/s/74rtpb6j7lzsf7nuz6ec
I have Corona public build 2014.2189
Is this a bug? Or am I doing something wrong?