Creating a score as an object goes off screen

Hello, could someone please check through this code and explain why a score is not being added when the object reaches set .y coordinate 

"local function spawnEnemy()

    local enemy = display.newImageRect(“images/zombie_”…imageInt…".png",50,54)

    enemy.x = mr( 20, 300 ); enemy.y = -30

    enemy.name = “enemy”; physics.addBody( enemy, { isSensor = true } )

    if enemy.y ==_H+45 then

    score = score + 10 – points for avoiding an enemy 

end"

the code of the spawning works and the zombie moves down the screen, my plan was to add a score as the zombie moved off screen (I was going to do some testing on this using trial and error with different y coordinates). 

Although I notice that no score is being added at all.

_H = Screen height 

Score is a variable that correctly changes from other sources of code, written the same format as above but this one is not functioning unlike the others. 

Any help would be highly appreciated. 

Many thanks, 

Daniel 

You might want to make the Y coordinate a range rather than a specific amount, like:

 if enemy.y > _H+45 then

Also, you are only calling the spawnEnemy function once, therefore the score addition function is only being called once. This will keep the code you have wrapped in the spawnEnemy function from being effective.

You’re going to need to have some kind of listener (Runtime, normally) with a function running constantly which looks for the Y coordinate of each object. You could also have a object specific Runtime listener that gets removed when you delete the object. If it were me, I’d do something like this:

 local function spawnEnemy()

    local enemy = display.newImageRect(“images/zombie_”…imageInt…".png",50,54)

    enemy.x = mr( 20, 300 ); enemy.y = -30

    enemy.name = “enemy”; physics.addBody( enemy, { isSensor = true } )

local function checkLocation()

    if enemy.y ==_H+45 then

Runtime:removeEventListener(“enterFrame”, checkLocation)

    score = score + 10 – points for avoiding an enemy 

end

Runtime:addEventListener(“enterFrame”, checkLocation)

end

That might add some significant complexity to your app, but it should get the job done. 

I attempted to use this code into my application although there have been no signs of difference. I have added a print function to detect whether the code is picking up the y coordinate being passed or not. It seems as though the code is not picking up that the image is passing _H+45

Try changing this piece

You’ll want to change:

 if enemy.y ==_H+45 then

To: 

 if enemy.y >_H+45 then

Let us know how you get on.

 
 
I have written this code;

local function spawnEnemy()

    local imageInt = mr(1,2)

    local enemy = display.newImageRect(“images/zombie_”…imageInt…".png",50,54)

    enemy.x = mr( 20, 300 ); enemy.y = -30

    enemy.name = “enemy”; physics.addBody( enemy, { isSensor = true } )

    enemyGroup:insert( enemy )

    local function checkLocation()

    if enemy.y >= _H+45 then

Runtime:removeEventListener(“enterFrame”, checkLocation)

    score = score -20 – points for avoiding an enemy 

    print “Passed”

end

Runtime:addEventListener(“enterFrame”, checkLocation)

end

There seems to be no response from the code as to the ‘enemy’ having passed the screen. 

Appreciating the help though thanks :slight_smile:

Maybe we need to talk about the _H variable. I’d suggest testing the code with a  static amount you know is 30 px in front of the enemy, to confirm the code itself is working correctly

Thanks for the assistance, I ended up taking a different approach to add the score as the item goes off screen.

I made a shape outside of the screen where the enemy submerges from, then created a collision between the two which inevitably adds a score.

I appreciate the help highly :slight_smile:

see you around the forums 

You might want to make the Y coordinate a range rather than a specific amount, like:

 if enemy.y > _H+45 then

Also, you are only calling the spawnEnemy function once, therefore the score addition function is only being called once. This will keep the code you have wrapped in the spawnEnemy function from being effective.

You’re going to need to have some kind of listener (Runtime, normally) with a function running constantly which looks for the Y coordinate of each object. You could also have a object specific Runtime listener that gets removed when you delete the object. If it were me, I’d do something like this:

 local function spawnEnemy()

    local enemy = display.newImageRect(“images/zombie_”…imageInt…".png",50,54)

    enemy.x = mr( 20, 300 ); enemy.y = -30

    enemy.name = “enemy”; physics.addBody( enemy, { isSensor = true } )

local function checkLocation()

    if enemy.y ==_H+45 then

Runtime:removeEventListener(“enterFrame”, checkLocation)

    score = score + 10 – points for avoiding an enemy 

end

Runtime:addEventListener(“enterFrame”, checkLocation)

end

That might add some significant complexity to your app, but it should get the job done. 

I attempted to use this code into my application although there have been no signs of difference. I have added a print function to detect whether the code is picking up the y coordinate being passed or not. It seems as though the code is not picking up that the image is passing _H+45

Try changing this piece

You’ll want to change:

 if enemy.y ==_H+45 then

To: 

 if enemy.y >_H+45 then

Let us know how you get on.

 
 
I have written this code;

local function spawnEnemy()

    local imageInt = mr(1,2)

    local enemy = display.newImageRect(“images/zombie_”…imageInt…".png",50,54)

    enemy.x = mr( 20, 300 ); enemy.y = -30

    enemy.name = “enemy”; physics.addBody( enemy, { isSensor = true } )

    enemyGroup:insert( enemy )

    local function checkLocation()

    if enemy.y >= _H+45 then

Runtime:removeEventListener(“enterFrame”, checkLocation)

    score = score -20 – points for avoiding an enemy 

    print “Passed”

end

Runtime:addEventListener(“enterFrame”, checkLocation)

end

There seems to be no response from the code as to the ‘enemy’ having passed the screen. 

Appreciating the help though thanks :slight_smile:

Maybe we need to talk about the _H variable. I’d suggest testing the code with a  static amount you know is 30 px in front of the enemy, to confirm the code itself is working correctly

Thanks for the assistance, I ended up taking a different approach to add the score as the item goes off screen.

I made a shape outside of the screen where the enemy submerges from, then created a collision between the two which inevitably adds a score.

I appreciate the help highly :slight_smile:

see you around the forums