Sprite Not working

I have looked at the various sprite tutorials on youtube and have followed them to the dot. I can’t get this to work even though it seems I’m using the right code and I have tried doing it the “old” way and the new way. Perhaps I am just misplacing things? :frowning:

*Lines 56-64 are the important ones*

(I only included the necessary portion of code)

There are 11 different frames in my sprite, and I created a sprite data sheet as well in Zwoptex.

[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()


– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
local bird


– BEGINNING OF YOUR IMPLEMENTATION

– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.


– move Bird function
local function moveBird(event)
if event.phase == “began” then
local target = event.target – this is the bird that was touched
target:applyLinearImpulse( .3 , -1.3, bird.x, bird.y)
end
return true
end

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

– create the level background
local background = display.newImageRect( “Stage1BG.jpg”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0

– make a bird (off-screen), position it, and rotate slightly
bird = display.newImage( “Bird2Side.png” )
bird.x = display.contentWidth/65 + 30
bird.y = display.contentHeight/2

– add physics to the bird
physics.addBody( bird, “dynamic”, { density=0.2, friction=0.3, bounce=0.4, radius=22 } )

–add an event listener to the bird
bird:addEventListener( “touch”, moveBird )

–allow sprites
require (“sprite”)
local data = require(“NewBlock”).getSpriteSheetData()
local sheet1 = sprite.newSpriteSheet( “NewBlock.png”, data )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 11)
local instance1 = sprite.newSprite( spriteSet1? )
instance1.x = display.contentWidth / 2
instance1.y = display.contentHeight / 2
instance1:play()

local startingBranch = display.newImage( “StartingBranch.png” )
startingBranch.x = display.contentWidth/65
startingBranch.y = display.contentHeight - 40
–add starting Branch as a physics body
physics.addBody(startingBranch, “static”, { friction=0.5 } )

local endingBranch = display.newImage(“EndingBranch.png”)
endingBranch.x = display.contentWidth
endingBranch.y = display.contentHeight - 40
–add ending Branch as a physics body
physics.addBody(endingBranch, “static”, { friction=0.5 })

local block1 = display.newImage(“WoodBlock1.png”)
block1.x = display.contentWidth/4
block1.y = display.contentHeight - 40
–add block 1 as physics body
physics.addBody( block1, “static”, { density=2.0, friction=0.3, bounce = .7 } )

local block2 = display.newImage(“WoodBlock2.png”)
block2.x = display.contentWidth/2
block2.y = display.contentHeight - 40
–add block 2 as physics body
physics.addBody( block2, “static”, { density = 1, friction = 2, bounce = .7, })
local block3 = display.newImage(“WoodBlock3.png”)
block3.x = display.contentWidth/1.33
block3.y = display.contentHeight - 40
–add block 3 as physics body
physics.addBody( block3, “static”, { density=2.0, friction=0.3, bounce = .7 } )

– all display objects must be inserted into group
group:insert( background )
group:insert( bird )
group:insert( block1 )
group:insert( block2 )
group:insert( block3 )
group:insert( startingBranch )
group:insert( endingBranch )
end[/lua]

[import]uid: 162953 topic_id: 29450 reply_id: 329450[/import]

You’re missing two things:

  1. after you define your sprite set, you have to do a series of calls to

sprite.add( SpriteSet, “Sprite1”, 1,10,600,0) – which defines the sequence called “Sprite1”, starting at sprite 1, for 10 frames, running for 600 milliseconds, and looping infinitely

  1. then, after you create your sprite with instance1 = sprite.newSprite( spriteSet1? ), you have to call

instance1:prepare(“Sprite1”) and only then you :play() it.

[import]uid: 160496 topic_id: 29450 reply_id: 118275[/import]

Just to add to this, there’s a good example of how to use sprites in CoronaSDK > SampleCode > Sprites > JungleScene :slight_smile: [import]uid: 52491 topic_id: 29450 reply_id: 118310[/import]

[lua] local options =
{
frames = require(“NewBlock”).frames,
}

– The new sprite API
local BlockSheet = graphics.newImageSheet( “NewBlock.png”, options )
local spriteOptions = { name=“NewBlock”, start=1, count=11, time=500 }
local spriteInstance = display.newSprite( BlockSheet, spriteOptions )

spriteInstance.x = contentWidth/2
spriteInstance.y = contentHeight/2

spriteInstance:play()[/lua]

thanks peach, I looked at that code and the horse animation code and this seems like it should work, but it isn’t.
If it helps my png sprite sheet is 512 by 256, but the sprites don’t take up all the space on the sheet, is that a problem? My sprite sheet does have 11 frames so I know I have that correct

The
[lua] local options =
{
frames = require(“NewBlock”).frames,
}[/lua]
Is a reference to the NewBlock.lua file which are instructions correct? I made that correctly as well. x_x [import]uid: 162953 topic_id: 29450 reply_id: 118352[/import]

I haven’t actually played with the brand new sprite API yet, I use the sample from 704. (The old sprite API still works fine and is in that one.)

Looking at this though I’m thinking what you have in NewBlock.lua should be shown here. Are you just loading the sheet from a separate file? [import]uid: 52491 topic_id: 29450 reply_id: 118472[/import]

yes it is a file containing the instructions that I made using zwoptex

contents
[lua]module(…)

– This file is for use with CoronaSDK™ and was generated by Zwoptex (http://zwoptexapp.com/)

– For more details, see http://developer.anscamobile.com/reference/sprite-sheets

function getSpriteSheetData()

local sheet = {
frames = {

{
name = “001.png”,
spriteColorRect = { x = 13, y = 2, width = 58, height = 46 },
textureRect = { x = 0, y = 0, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “002.png”,
spriteColorRect = { x = 17, y = 1, width = 56, height = 48 },
textureRect = { x = 92, y = 0, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “003.png”,
spriteColorRect = { x = 15, y = 0, width = 62, height = 50 },
textureRect = { x = 184, y = 0, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “004.png”,
spriteColorRect = { x = 11, y = 0, width = 70, height = 50 },
textureRect = { x = 276, y = 0, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “005.png”,
spriteColorRect = { x = 4, y = 0, width = 84, height = 50 },
textureRect = { x = 368, y = 0, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “006.png”,
spriteColorRect = { x = 2, y = 0, width = 88, height = 50 },
textureRect = { x = 0, y = 52, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “007.png”,
spriteColorRect = { x = 0, y = 0, width = 90, height = 50 },
textureRect = { x = 92, y = 52, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “008.png”,
spriteColorRect = { x = 5, y = 0, width = 84, height = 50 },
textureRect = { x = 184, y = 52, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “009.png”,
spriteColorRect = { x = 9, y = 0, width = 80, height = 50 },
textureRect = { x = 276, y = 52, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “010.png”,
spriteColorRect = { x = 16, y = 0, width = 72, height = 50 },
textureRect = { x = 368, y = 52, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

{
name = “011.png”,
spriteColorRect = { x = 21, y = 11, width = 50, height = 28 },
textureRect = { x = 0, y = 104, width = 90, height = 50 },
spriteSourceSize = { width = 90, height = 50 },
spriteTrimmed = false,
textureRotated = false
},

}
}

return sheet
end[/lua]

everything seems right [import]uid: 162953 topic_id: 29450 reply_id: 118566[/import]

  
local data = require("NewBlock").getSpriteSheetData()  
local options = {  
 frames = data.frames  
}  
  

if your using old sprite sheet data with the new sprite api’s thats how it should look :slight_smile: [import]uid: 84637 topic_id: 29450 reply_id: 118569[/import]