Accessing the sprite sheet

Hi,

So I have my sprite sheet declared , and I want to use it for multiple purposes without putting the sprite sheet inside a local function.

How do I do that?

e.g.

I want to use my sprite sheet in a loop and in a collision event.

Thanks,

Bob

Hi Bob,

Just forward declare (upvalue) the sprite sheet reference, and use it whenever needed.

Brent

Hi Brent,

 I do not know exactly how to upvalue an object,but I googled it and I did something like this:

Or can you please give me a tutorial/reference for upvalueing objects?

Thanks,

Bob

function cowAnim () local sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} local mySheet = graphics.newImageSheet ("cow.png",sheetData) local sequenceData = { {name = "cowWalking",start = 1,count = 8,time = 1300,loopCount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopCount = 1} } local cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() physics.addBody (cow,"dynamic",{radius = 20,isSensor = true }) sceneGroup:insert (cow) return function () return cow end end local function cowSpawn () for i = 0,0 do local c = cowAnim() ... end end

Hi Bob,

Have you learned everything about Lua “scope” at this point? This is an essential topic which you absolutely must understand, otherwise you will encounter many difficulties as you progress with Lua/Corona.

Here is a video on the topic:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Brent

Hi Brent,

Unfortunatelly,upvalueing my spritesheet didn’t work.

I still need to do the following actions with my sprite sheets,such as:

  1. Insert it into a for loop;

  2. Set a different sequance when the spritesheet has collided with another object

  3. eliminate it

Any idea how can I do that?

Thank you,

Bob

Hi Bob,

If the upvalue of the sheet doesn’t work, then most likely it wasn’t upvalued properly. I’d need to see your code for clearer understanding.

Brent

Hi Brent,

This is the code.

local sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} local mySheet = graphics.newImageSheet ("cow.png",sheetData) local sequenceData = { {name = "cowWalking",start = 1,count = 8,time = 1300,loopCount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopCount = 1} } local cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() physics.addBody (cow,"static",{radius = 20,isSensor = true }) sceneGroup:insert (cow) local function cowSpawn (event) for k = 0,0 do sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} mySheet = graphics.newImageSheet ("cow.png",sheetData) sequenceData = { {name = "cowWalking",start = 1,count = 8,time = 1300,loopCount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopCount = 1} } cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() cow.x = math.random (400,410) cow.y = math.random (283,290) physics.addBody (cow,"static",{radius = 20,isSensor = true }) sceneGroup:insert (cow) local function removeCow () display.remove (cow) cow = nil end cowTransition = transition.to (cow,{time = 6300,x = 90,onComplete = removeCow }) end end -- or -- local function spawnCows () --for i = 0,1 do --local c = cowAnim() //the sprite sheet --some other functions that will get the "c" removed when it reaches x = 0; -- a collision event listener cow:addEventListener ("collision",colFunc) --end end

Thank you

Hi @bogdanmocanu2,

You are repeating yourself (in code). You correctly upvalue things like “sheetData”, “mySheet”, and “sequenceData”. Later, however, you reset these variables within the “cowSpawn()” function, for no apparent reason (the code is exactly the same).

Simply reference the upvalue variables. That is the purpose of doing upvalues… you do them once, then you simply use/reference them later.

Brent

Hi Brent,

 Works like charm,but my my object cannot detect any collision.

What if I create a tbel with my sprite object e.g. 

local tabel = {local cow = ...} and then insert the table into the for loop. for i = 1,2 do local x = tabel[] ... end

local function CowCollision (event) if (event.phase == "began" ) then print ("hello") end end cow:addEventListener ("collision",CowCollision) local cowTable = {} local function cowSpawn (event) for k = 0,0 do local cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() cow.x = math.random (400,410) cow.y = math.random (283,290) sceneGroup:insert (cow) local function removeCow () display.remove (cow) cow = nil end cowTransition = transition.to (cow,{time = 6300,x = 90,onComplete = removeCow }) end end 

Thank you,

Bob

Hi Bob,

I still see that the main issue is scope problems. Perhaps these resources will help explain it in more detail:

http://lua-users.org/wiki/ScopeTutorial

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

I know this topic may seem complicated, but it’s essential that you understand it 100%, because it is so fundamental to programming.

Take care,

Brent

In specific to the code block above, look at this line…

[lua]

cow:addEventListener (“collision”,CowCollision)

[/lua]

Now ask yourself, what is “cow”? Have you declared that object in the proper scope above that line? The answer is “no”, because you have not created any object named “cow” until lower down in the “cowSpawn()” function. So, Lua does not know what you’re referring to, and thus no collisions.

Brent

Hi Brent,

It worked,thanks for the tutorials,and patience :slight_smile:

Bob

Hi Bob,

Just forward declare (upvalue) the sprite sheet reference, and use it whenever needed.

Brent

Hi Brent,

 I do not know exactly how to upvalue an object,but I googled it and I did something like this:

Or can you please give me a tutorial/reference for upvalueing objects?

Thanks,

Bob

function cowAnim () local sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} local mySheet = graphics.newImageSheet ("cow.png",sheetData) local sequenceData = { {name = "cowWalking",start = 1,count = 8,time = 1300,loopCount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopCount = 1} } local cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() physics.addBody (cow,"dynamic",{radius = 20,isSensor = true }) sceneGroup:insert (cow) return function () return cow end end local function cowSpawn () for i = 0,0 do local c = cowAnim() ... end end

Hi Bob,

Have you learned everything about Lua “scope” at this point? This is an essential topic which you absolutely must understand, otherwise you will encounter many difficulties as you progress with Lua/Corona.

Here is a video on the topic:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Brent

Hi Brent,

Unfortunatelly,upvalueing my spritesheet didn’t work.

I still need to do the following actions with my sprite sheets,such as:

  1. Insert it into a for loop;

  2. Set a different sequance when the spritesheet has collided with another object

  3. eliminate it

Any idea how can I do that?

Thank you,

Bob

Hi Bob,

If the upvalue of the sheet doesn’t work, then most likely it wasn’t upvalued properly. I’d need to see your code for clearer understanding.

Brent

Hi Brent,

This is the code.

local sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} local mySheet = graphics.newImageSheet ("cow.png",sheetData) local sequenceData = { {name = "cowWalking",start = 1,count = 8,time = 1300,loopCount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopCount = 1} } local cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() physics.addBody (cow,"static",{radius = 20,isSensor = true }) sceneGroup:insert (cow) local function cowSpawn (event) for k = 0,0 do sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} mySheet = graphics.newImageSheet ("cow.png",sheetData) sequenceData = { {name = "cowWalking",start = 1,count = 8,time = 1300,loopCount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopCount = 1} } cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() cow.x = math.random (400,410) cow.y = math.random (283,290) physics.addBody (cow,"static",{radius = 20,isSensor = true }) sceneGroup:insert (cow) local function removeCow () display.remove (cow) cow = nil end cowTransition = transition.to (cow,{time = 6300,x = 90,onComplete = removeCow }) end end -- or -- local function spawnCows () --for i = 0,1 do --local c = cowAnim() //the sprite sheet --some other functions that will get the "c" removed when it reaches x = 0; -- a collision event listener cow:addEventListener ("collision",colFunc) --end end

Thank you

Hi @bogdanmocanu2,

You are repeating yourself (in code). You correctly upvalue things like “sheetData”, “mySheet”, and “sequenceData”. Later, however, you reset these variables within the “cowSpawn()” function, for no apparent reason (the code is exactly the same).

Simply reference the upvalue variables. That is the purpose of doing upvalues… you do them once, then you simply use/reference them later.

Brent

Hi Brent,

 Works like charm,but my my object cannot detect any collision.

What if I create a tbel with my sprite object e.g. 

local tabel = {local cow = ...} and then insert the table into the for loop. for i = 1,2 do local x = tabel[] ... end

local function CowCollision (event) if (event.phase == "began" ) then print ("hello") end end cow:addEventListener ("collision",CowCollision) local cowTable = {} local function cowSpawn (event) for k = 0,0 do local cow = display.newSprite (mySheet,sequenceData) cow:setSequence ("cowWalking") cow:play() cow.x = math.random (400,410) cow.y = math.random (283,290) sceneGroup:insert (cow) local function removeCow () display.remove (cow) cow = nil end cowTransition = transition.to (cow,{time = 6300,x = 90,onComplete = removeCow }) end end 

Thank you,

Bob