Help on Sprites

Hi everyone…

I have a sprite that is moving everywhere…by the way

I don’t know if this is the best way, but it works

    sequenceFish = {     { name="fishy", start=1, count=6, time=800 } -- loopDirection = "bounce"     }          fishSheet = graphics.newImageSheet( "images/spriteFish.png", { width=200, height=105, numFrames=6 } )          local function swimming()         if fishSprite then             fishSprite:removeSelf()             fishSprite = nil         end         --(math.random(-50, 50))     fishSprite = display.newSprite( fishSheet, sequenceFish )     sceneGroup:insert(fishSprite)     fishSprite.x = display.contentCenterX - 300     fishSprite.y = display.contentCenterY + (math.random(-150, 150))     fishSprite.xScale = .4     fishSprite.yScale = .4     fishSprite.alpha = .3     fishSprite.rotation = 0     fishSprite:play()     local function backFish()     fishSprite.rotation = 180     transition.to(fishSprite, {time=9000, x=display.contentCenterX - 300, onComplete=swimming})     end     transition.to(fishSprite, {time=9000, x=display.contentWidth  + (math.random(-150, 150)), y=display.contentCenterY + (math.random(-150, 150)), onComplete=backFish})     end     swimming()

– QUESTION –

when I go to another scene in composer

After a few seconds, I guess when the transition is complete

I get the error

File: mapa.lua
Line: 67
Attempt to index upvalue ‘fishSprite’ (a nil value)

I don’t find the sprite in the other scene …

so in the “hide” scene I am trying this but still is not working

            if fishSprite then             fishSprite:pause()             fishSprite:removeSelf()             fishSprite = nil             end

Thanks for your help

---- Bonus Question For Challenging Programmers—

If you look at the code, the fish is traveling from point A to point B

in a straight line.

but the point of the fish is looking weird, because of the [rotation]

There is a way to detect if the math.random gets 98

then rotate like - 34 degerees

so automatically the fish follows the direction of the line

– I know, this is for VERY advanced programmers,

I just wanted to see if someone can actually do this

For your main question, it looks to me like the transition is completing after you move to the next scene. If you inserted fishsprite into scene.view then the image for fishSprite will automatically be removed when moving to the next scene, but fishSprite may not be nil. As a result :removeSelf is getting called on an image that no longer exists.

In order to avoid this, I think you should put

transition.cancel(fishSprite) display.remove(fishSprite) fishSprite = nil

in the “did” phase of scene:hide(). This will clean up fishSprite and prevent the onComplete function from firing later after moving to the new scene.

Thank you very much Vince… that works well!

So I needed to cancel the transition… I got it!

Thanks

For your main question, it looks to me like the transition is completing after you move to the next scene. If you inserted fishsprite into scene.view then the image for fishSprite will automatically be removed when moving to the next scene, but fishSprite may not be nil. As a result :removeSelf is getting called on an image that no longer exists.

In order to avoid this, I think you should put

transition.cancel(fishSprite) display.remove(fishSprite) fishSprite = nil

in the “did” phase of scene:hide(). This will clean up fishSprite and prevent the onComplete function from firing later after moving to the new scene.

Thank you very much Vince… that works well!

So I needed to cancel the transition… I got it!

Thanks