Multiply score by 2

Can someone help me on this code i am trying to multiply the score by two if “double” collides with “penguin” .
[lua]local function spawnDouble()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
double = display.newImage( “x2.png” )
double.x = randomPos
double.y = -100
double.myName = “double”
physics.addBody(double , “dynamic”, {isSensor = true, radius = 15})
localGroup:insert( double )
end
end
doubleTimer = timer.performWithDelay( 1500, spawnDouble, 0 )

local function penguinCollision (event)
if event.phase == “began” and event.other.myName == “double” then
event.other:removeSelf()
score = score * 2
updateScore()
end
end
penguin:addEventListener(“collision”, penguinCollision)
localGroup:insert( double ) [import]uid: 132369 topic_id: 25581 reply_id: 325581[/import]

Hi,

You should probably put parenthesis around your two comparisons on line 15, and put in a print statement so you can see if the collision is being registered (in case the parenthesis don’t fix it).

like this:

[lua]local function spawnDouble()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
double = display.newImage( “x2.png” )
double.x = randomPos
double.y = -100
double.myName = “double”
physics.addBody(double , “dynamic”, {isSensor = true, radius = 15})
localGroup:insert( double )
end
end
doubleTimer = timer.performWithDelay( 1500, spawnDouble, 0 )

local function penguinCollision (event)
print("penguinCollision event.phase = ", event.phase)
if (event.phase == “began”) and (event.other.myName == “double”) then
print(“penguin collided with double!”)
event.other:removeSelf()
score = score * 2
updateScore()
end
end
penguin:addEventListener(“collision”, penguinCollision)
localGroup:insert( double ) [import]uid: 10211 topic_id: 25581 reply_id: 103385[/import]

Yes, but i keep on getting this error

[code]

Director ERROR: Failed to execute new( params ) function on ‘level1’.

/Users/Mazensalih/Documents/penguin-Peach/level1.lua:111: attempt to index global ‘penguin’ (a nil value)
----------------------- [import]uid: 132369 topic_id: 25581 reply_id: 103390[/import]

Never mind i have got it working :slight_smile: [import]uid: 132369 topic_id: 25581 reply_id: 103397[/import]

Ok, that definitely changes things. There’s an issue going on with the penguin variable. Look over the code you have concerning the penguin carefully. Starting with where you define it. Most likely your problem is one of these:

  1. Penguin isn’t defined properly.

  2. Penguin is out of scope on line 111.

  3. Penguin is deleted before line 111.

Matt
W2MD [import]uid: 10211 topic_id: 25581 reply_id: 103398[/import]

ha! [import]uid: 10211 topic_id: 25581 reply_id: 103399[/import]