2
\$\begingroup\$

Working on a game in the Corona SDK with Lua and I need help so the player animates on the line drawn. Right now he animates at the beggining and the end but not on the line.

Here is the player code:

function spawnPlayerObject(xPlayerSpawn,yPlayerSpawn,richTurn) --spawns Rich where we need him.
-- riches sprite sheet and all his inital spirites. We need to adjust this so that animations 3-6 are the only ones that animate when moving horizontally
    local richPlayer = sprite.newSpriteSet(richSheet1,1,6)
    sprite.add(richPlayer, "rich", 1,6,500,1)
    rich = sprite.newSprite(richPlayer)
    rich.x = xPlayerSpawn
    rich.y = yPlayerSpawn
    rich:scale(_W*0.0009, _W*0.0009) -- scale is used to adjust the size of the object.
    richDir = richTurn
    rich.rotation = richDir
    rich:prepare("rich")
    rich:play()
    physics.addBody( rich, "static", { friction=1, radius = 15 } ) -- needs a better physics body for collision detection.
end

And here is the code for the line:

function movePlayerOnLine(event)
    --for the original image replace all rich with player.
 if posCount ~= linePart then
    richDir = math.atan2(ractgangle_hit[posCount].y-rich.y,ractgangle_hit[posCount].x-rich.x)*180/math.pi
    playerSpeed = 5
    rich.x = rich.x + math.cos(richDir*math.pi/180)*playerSpeed
    rich.y = rich.y + math.sin(richDir*math.pi/180)*playerSpeed
        if rich.x < ractgangle_hit[posCount].x+playerSpeed*10 and rich.x > ractgangle_hit[posCount].x-playerSpeed*10 then
            if rich.y < ractgangle_hit[posCount].y+playerSpeed*10 and rich.y > ractgangle_hit[posCount].y-playerSpeed*10 then
            posCount = posCount + 1
        end
    end

I don't think anything has changed recently but I have been getting an error when testing of "attempt to upvalue "rich" a nil value" on the second line, richDir = etc.

\$\endgroup\$
0
\$\begingroup\$

Is movePlayerOnLine getting called before spawnPlayerObject? That would explain why rich is nil as it hasn't been assigned a sprite object yet.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ no spawnPlayerObject is before movePlayerOnLine \$\endgroup\$ – andrew McCutchan Oct 28 '11 at 1:05
  • \$\begingroup\$ the error has been fixed but he is still not animating on the line \$\endgroup\$ – andrew McCutchan Oct 28 '11 at 4:28
  • \$\begingroup\$ Has the line or ractgangle_hit have a physics body? Do you have something setup so it plays an animation if there is a physics collision? \$\endgroup\$ – yaustar Oct 28 '11 at 10:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.