unable to see sprites when removeSelf is called

I am having an issue with a sprite test. When I do not use removeSelf() my sprite sequence shows fine. However if I have a call following the play() command to the removeSelf() function,I see no sprite image at all. Since the up loader will not allow me to send the code any other way here it is.

Main:


– Sprites


local newPics = require(‘classes.spriteFunctions’)
– forward declarations and other locals
–local newAcorn = require(‘classes.acorn’).newAcorn – The acorn character
–local acorn = {}

– parameter test for sequence data parameters
local loops = 15
local runTime = 1500
local spriteX = 300
local spriteY = 700


– Background

local newBackGround = display.newRect(1500,800,3000,1600)
newBackGround:setFillColor(0.5)

– call spriteFunction flip pecan

spriteX = 300
spriteY = 700
runTime = 500
loops = 10
newPics.spriteSequences(loops, runTime, spriteX, spriteY,“compress”)
newPics.cleanup() – call remove self

– End playSequence

sprite definitions:


– created with TexturePacker

local SheetInfo = {}

SheetInfo.sheet =
{
    frames = {
    
        {
            – Nut-Compress0001
            x=268,
            y=70,
            width=64,
            height=115,

        },
        {
            – Nut-Compress0002
            x=334,
            y=70,
            width=64,
            height=115,

        },
        {
            – Nut-Compress0003
            x=466,
            y=70,
            width=64,
            height=111,

            sourceX = 0,
            sourceY = 4,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0004
            x=532,
            y=67,
            width=64,
            height=109,

            sourceX = 0,
            sourceY = 6,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0005
            x=598,
            y=67,
            width=64,
            height=103,

            sourceX = 0,
            sourceY = 11,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0006
            x=664,
            y=65,
            width=64,
            height=101,

            sourceX = 0,
            sourceY = 13,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0007
            x=730,
            y=59,
            width=64,
            height=97,

            sourceX = 0,
            sourceY = 16,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0008
            x=796,
            y=59,
            width=64,
            height=93,

            sourceX = 0,
            sourceY = 20,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0009
            x=796,
            y=154,
            width=64,
            height=91,

            sourceX = 0,
            sourceY = 22,
            sourceWidth = 64,
            sourceHeight = 115
        },
        {
            – Nut-Compress0010
            x=730,
            y=158,
            width=64,
            height=87,

            sourceX = 0,
            sourceY = 25,
            sourceWidth = 64,
            sourceHeight = 115
        },
    },
    
    sheetContentWidth = 861,
    sheetContentHeight = 252
}

SheetInfo.frameIndex =
{

    [“Nut-Compress0001”] = 1,
    [“Nut-Compress0002”] = 2,
    [“Nut-Compress0003”] = 3,
    [“Nut-Compress0004”] = 4,
    [“Nut-Compress0005”] = 5,
    [“Nut-Compress0006”] = 6,
    [“Nut-Compress0007”] = 7,
    [“Nut-Compress0008”] = 8,
    [“Nut-Compress0009”] = 9,
    [“Nut-Compress0010”] = 10
}

function SheetInfo:getSheet()
    return self.sheet;
end

function SheetInfo:getFrameIndex(name)
    return self.frameIndex[name];
end

return SheetInfo

SpritFunctions:


– SETUP

local sheetInfo = require( ‘classes.Nut_Sprites’ )
– Create the image sheet
local imageSheet = graphics.newImageSheet( ‘image/Nut_Sprites.png’,
    sheetInfo:getSheet( ) )

local spriteOps = {}

spriteOps.spriteSequences = function(loops,runTime,spriteX,spriteY,action)
    local loops   = loops
    local runTime = runTime
    local spriteX = spriteX
    local spriteY = spriteY
    local action = action

– define the sprite image display sequences, cycle time, cycle counts

local sequenceData = {
    {
        name = “compress”,
        start = 1,
        count = 10,
        time = runTime,
        loopCount = loops
    }
    
}
– End sequenceData

– set up new sprite object definition

    local mySprite = display.newSprite( imageSheet, sequenceData )

spriteOps.cleanUp = function()
    print(‘in cleanup’)
    mySprite:removeSelf()
    mySprite = nil
end

– execute called sprite

    mySprite:setSequence( action )
    mySprite:play( )
    mySprite.x = spriteX
    mySprite.y = spriteY
       
   --mySprite:removeSelf( )

end
return spriteOps
 

As I said neither up loader would allow the .lua files to be loaded so I am forced to provide them here in this format. It did however accept the texture packer sprite sheet.

Sprite playing is non-blocking, so you’re effectively saying:

  • start playing at time X
  • delete at time X

What you really seem to want is

  • start playing at time X
  • delete at time Y, where Y == X + time to play

Use a callback (listener) on the sprite to delete it.

ok being new to all of this I am not sure where the listener needs to go, looks like the examples are placing it in the code I am calling ‘sprite functions’. I am unsure on this point. If you could clarify where the code should go I would appreciate it

SpritFunctions:


– SETUP

local sheetInfo = require( ‘classes.Nut_Sprites’ )
– Create the image sheet
local imageSheet = graphics.newImageSheet( ‘image/Nut_Sprites.png’,
    sheetInfo:getSheet( ) )

local spriteOps = {}

spriteOps.spriteSequences = function(loops,runTime,spriteX,spriteY,action)
    local loops   = loops
    local runTime = runTime
    local spriteX = spriteX
    local spriteY = spriteY
    local action = action

– define the sprite image display sequences, cycle time, cycle counts

local sequenceData = {
    {
        name = “compress”,
        start = 1,
        count = 10,
        time = runTime,
        loopCount = loops
    }
    
}
– End sequenceData

– set up new sprite object definition

    local mySprite = display.newSprite( imageSheet, sequenceData )

spriteOps.cleanUp = function()
    print(‘in cleanup’)
    mySprite:removeSelf()
    mySprite = nil
end

– execute called sprite

    mySprite:setSequence( action )
    mySprite:play( )
    mySprite.x = spriteX
    mySprite.y = spriteY
       

local function mySpriteListener( event )

if (event.phase == “ended”) then

mySprite.cleanUp()

end

print('event.phase: ',event.phase)

end


mySprite:addEventListener( “sprite”, mySpriteListener )

   --mySprite:removeSelf( )

end
return spriteOps
 

Something like this:

mySprite.sprite = function( self, event ) if( event.phase == "ended" ) then display.remove(self) end end mySprite:addEventListener( "sprite" ) mySprite:play( ) 

In the future, please format your code when you post, this makes it easier to read and answer you (also read it after you post and don’t be afraid to go back and fix formatting.  The easier it is to read the more likely you are to get a answer.  Time is $$ for most of us).  

In this case your code was fairly easy to read, so that’s cool.  However, I’m asking anyways.

formatyourcode.jpg

Sprite playing is non-blocking, so you’re effectively saying:

  • start playing at time X
  • delete at time X

What you really seem to want is

  • start playing at time X
  • delete at time Y, where Y == X + time to play

Use a callback (listener) on the sprite to delete it.

ok being new to all of this I am not sure where the listener needs to go, looks like the examples are placing it in the code I am calling ‘sprite functions’. I am unsure on this point. If you could clarify where the code should go I would appreciate it

SpritFunctions:


– SETUP

local sheetInfo = require( ‘classes.Nut_Sprites’ )
– Create the image sheet
local imageSheet = graphics.newImageSheet( ‘image/Nut_Sprites.png’,
    sheetInfo:getSheet( ) )

local spriteOps = {}

spriteOps.spriteSequences = function(loops,runTime,spriteX,spriteY,action)
    local loops   = loops
    local runTime = runTime
    local spriteX = spriteX
    local spriteY = spriteY
    local action = action

– define the sprite image display sequences, cycle time, cycle counts

local sequenceData = {
    {
        name = “compress”,
        start = 1,
        count = 10,
        time = runTime,
        loopCount = loops
    }
    
}
– End sequenceData

– set up new sprite object definition

    local mySprite = display.newSprite( imageSheet, sequenceData )

spriteOps.cleanUp = function()
    print(‘in cleanup’)
    mySprite:removeSelf()
    mySprite = nil
end

– execute called sprite

    mySprite:setSequence( action )
    mySprite:play( )
    mySprite.x = spriteX
    mySprite.y = spriteY
       

local function mySpriteListener( event )

if (event.phase == “ended”) then

mySprite.cleanUp()

end

print('event.phase: ',event.phase)

end


mySprite:addEventListener( “sprite”, mySpriteListener )

   --mySprite:removeSelf( )

end
return spriteOps
 

Something like this:

mySprite.sprite = function( self, event ) if( event.phase == "ended" ) then display.remove(self) end end mySprite:addEventListener( "sprite" ) mySprite:play( ) 

In the future, please format your code when you post, this makes it easier to read and answer you (also read it after you post and don’t be afraid to go back and fix formatting.  The easier it is to read the more likely you are to get a answer.  Time is $$ for most of us).  

In this case your code was fairly easy to read, so that’s cool.  However, I’m asking anyways.

formatyourcode.jpg