Attempt to perform aithmetic on field 'y' (a nil value)

Hi,

I recive this error each time my main character hits an enemy and the level switches.I tried removing the event listener before switching to another level,but it didn’t work.Any ideas?

The variable squid is previous declared and it’s y possition is 150.

Thank you,

Bogdan

local function movement (event) squid.y = squid.y + motion -- 319 line end

Hi Bogdan,

The only logical reason for this is that you’re trying to read the .y sometime after (or before) “squid” is an actual object. So perhaps you set it to nil, then you tried to detect .y, but of course there is no .y after it’s been nil’led out.

Are you absolutely sure that you’re removing the “movement” function event listener from the squid before anything else happens?

Brent

Hi Brent,

Indeed ‘squid’ is an object. In order to remove the ‘movement’ function I used this line:
Runtime:removeEventListener(“enterFrame”,movement)

Cheers,
Bogdan

Hi Bogdan,

OK, so around the same time, you also remove the “squid” object? If so, try just conditionally checking that the squid still exists:

[lua]

local function movement (event)

   if squid then

      squid.y = squid.y + motion

   end

end

[/lua]

Alternatively, you might try removing the movement listener, then waiting about 20 milliseconds before going to the next scene.

Brent

Hi Brent,
I fixed the error,thanks!

Can you please recomend me a tutorial on how to create a sine movement for a enemy?

Thanks,
Bogdan

Hi Bogdan,

I would suggest writing a function that transitions (transition.to()) the enemy in one direction, with an outQuad easing, then when that completes (using the onComplete event for the transition), send the enemy back in the other direction on a similar transition. And keep repeating it. :slight_smile:

Brent

Hi Bogdan,

The only logical reason for this is that you’re trying to read the .y sometime after (or before) “squid” is an actual object. So perhaps you set it to nil, then you tried to detect .y, but of course there is no .y after it’s been nil’led out.

Are you absolutely sure that you’re removing the “movement” function event listener from the squid before anything else happens?

Brent

Hi Brent,

Indeed ‘squid’ is an object. In order to remove the ‘movement’ function I used this line:
Runtime:removeEventListener(“enterFrame”,movement)

Cheers,
Bogdan

Hi Bogdan,

OK, so around the same time, you also remove the “squid” object? If so, try just conditionally checking that the squid still exists:

[lua]

local function movement (event)

   if squid then

      squid.y = squid.y + motion

   end

end

[/lua]

Alternatively, you might try removing the movement listener, then waiting about 20 milliseconds before going to the next scene.

Brent

Hi Brent,
I fixed the error,thanks!

Can you please recomend me a tutorial on how to create a sine movement for a enemy?

Thanks,
Bogdan

Hi Bogdan,

I would suggest writing a function that transitions (transition.to()) the enemy in one direction, with an outQuad easing, then when that completes (using the onComplete event for the transition), send the enemy back in the other direction on a similar transition. And keep repeating it. :slight_smile:

Brent