Rotation Error

The same error appears when I make spaceship a global variable. Here is some more output from the error:

levels.lua:64: in function ‘_listener’

Line 64 is the code, ‘spaceship.rotation = spaceship.rotation + 45’. Any more ideas?

Try, right after you create the spaceship, putting

[lua]

print(spaceship, spaceship.rotation)

[/lua]

and then another one in the rotation function. See what’s printed.

C

I tried out the print statements, and the console prints it out just fine: spaceship, spaceship.rotation. The function is definitely being executed. Only up until the end, the simulator calls a Runtime error and halts the program, and the same error as before appears.  

The console should print something like “table: 0x10d364110” for the spaceship and a number for the rotation. Is it printing that? Or is it printing what you posted - “spaceship”, “spaceship.rotation”?

C

When I don’t put the print statement in quotes, this appears:

table: 0x10bef6b70

When I put it in quotes, “spaceship, spaceship.rotation” appears. 

Ravipsr

Ravipsr putting anything in quotes will print a string not the value of the variable, table, function, or whatever the object is.

You should print the variables without quotes.

So, as you can see, the spaceship’s rotation is being “string-ized” or something to that effect. It’s printing nothing for the rotation.

So do a command+F search for “spaceship.rotation=” in your text editor and see if you’re changing it to anything you don’t want to be changing it to.

Sorry, I didn’t put the whole output from the console: table: 0x10ac33500 12105.015625. So is this just a value for the function? 

I did a command search for “spaceship.rotation =”. The only two occurrences of it are in the code I posted above, so it is not being changed into anything I don’t want. 

I apologize for not understanding, but how will this help solve the error I’m getting?

Whole output… I thought I was on to something ;D

Any way you can post up more code?

Doing the command+F search for “spaceship.rotation=” was so that you could find it if, for example, you were doing

[lua]

spaceship.rotation=nil

[/lua]

which would most likely be the problem.

You might also try taking out that “spaceship.rotation=spaceship.rotation” as I’m not sure that makes a difference.

Also try printing the spaceship’s rotation inside of the spaceshipAnimation function and see what happens.

[lua]

local function spaceshipAnimation()

    print(spaceship.rotation)

    spaceship.rotation = spaceship.rotation + 45

end

[/lua]

C

Here is all my code, from creating the spaceship to its rotation function:

[lua]local spaceship = display.newImageRect(“SGSpacecraft2.png”, 40, 40)

    spaceship.x = display.contentWidth/2 - 125

    spaceship.y = display.contentHeight/2 

    physics.addBody(spaceship,“dynamic”)

    spaceship.name = “spaceship”

    print(spaceship,spaceship.rotation)

    

    local function spaceshipAnimation()

        spaceship.rotation = spaceship.rotation + 45

        print(spaceship.rotation)

    end

    

    satmr = timer.performWithDelay(200,spaceshipAnimation,-1)[/lua]

I had already tried removing the spaceship.rotation = spaceship.rotation, but it didn’t make a difference. I changed the print statement to just spaceship.rotation in the function, and this time only the numbers were popping up; no ‘table’. It was incrementing by 45 each time. The output is:

Corona Simulator: 45

Corona Simulator: 90

Corona Simulator: 135

Corona Simulator: 180

Corona Simulator: 225

Corona Simulator: 270

Corona Simulator: 315

Corona Simulator: 360

Corona Simulator: 405

Corona Simulator: 449.99996948242

Corona Simulator: 494.99996948242

@ravipsr

Looking at your last reply it appears the spaceship is rotating. So when exactly does this error happen? If you do nothing does it happen randomly or do you have physics collisions happening. There is obviously more to it than just a spaceship rotating. Something in the code is causing spaceship to nil out.

Do you have a collision function that removes the spaceship if it hits something or something hits it? If you remove the spaceship, you need to kill the timer or it will attempt to rotate the spaceship that no longer exists.

Once a display object is converted to a physics object, you cannot directly rotate the object using object.rotation or object:rotate(). The object will rotate but the physics part of the object won’t rotate. You can see this by turning on physics.setDrawMode().

@Anderoth

Wow. You’re absolutely right! I forgot to remove the timer in my collision function, and it was trying to rotate a nil spaceship. After I put in timer.cancel(satmr), the error went away.

Sorry, I’m only 15 years old, so I am not very experienced in programming yet, and it’s always the little errors that get me. Thank you everyone for all your help!

-Nishanth S.