简体   繁体   中英

attempt to call method 'translet' (a nil value) in corona

For this function fallingCoins() , I'm getting backward moving coins in my code and I need to hide these coins when anchor touches the coins.

function fallingCoins()
local myPlayer = display.newCircle( math.random(20,_W+20), -25, math.random(10,10)  )
    myPlayer:setStrokeColor(255, 128, 0 ) 
    myPlayer:setFillColor(math.random(245,255),math.random(210,223),7)
    myPlayer.myName = "myPlayer"
    physics.addBody( myPlayer, "static" )
    myPlayer.y = "150"
     local function muovi()
     myPlayer:translate(-2, 0)
     end
 Runtime:addEventListener( "enterFrame", muovi );

end
timer.performWithDelay( 3000, fallingCoins )

And I'm hiding the coins if it touch anchor.

function onCollision3( event )
   if(event.object1.myName == "guy" and event.object2.myName == "myPlayer") then
      event.object2:removeSelf(); 
   end
end
Runtime:addEventListener( "collision", onCollision3 )

If I touch coins then I'm getting this error

"---------------------------
Corona Runtime Error
---------------------------
...as\desktop\run2\scroll\scrolling background\main.lua:123: attempt to call method 'translate' (a nil value)
stack traceback:
    [C]: in function 'translate'
    ...as\desktop\run2\scroll\scrolling background\main.lua:123: in function <...as\desktop\run2\scroll\scrolling background\main.lua:122>
    ?: in function <?:218>

Do you want to relaunch the project?
---------------------------
Yes   No   
---------------------------
"

please help me where I'm doing mistake..

translate method can be applied to display objects. Here your objects is also a physics, objects. You can change problematic line with:

myPlayer.x = myPlayer.x - 2

PS : But in this case it will move soo fast^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM