My player has three states, moving left, moving right, and not moving (each are just a single image). Initially the player image is set with player = display.newImageRect to “not moving” and physics is applied for collision purposes. Now I want just change the image associated with the player if “moving left” or “moving right” based on events, but calling player = display.newImageRect totally breaks things, and I don’t see a way to just change the underlying image associated with the player. Do I need to implement a SpriteObject despite the fact I have no real animations?
Having animations that are 1 frame long are perfectly acceptable. Sprites give you various states that images can be in, such as “running”, “jumping” “moveleft”, “idle”, etc. Each of these states can have 1 or more images that make up the animation and it’s a convenient way to have your character show the left facing version, right facing version or idle version.
If not, you need to have three display.newImageRect()'s and hide two, show one, but move all three in unison. When you change state, you would hide two, show one etc. It can be done but sprites do a lot of that work for you.
Rob
In addition to what Rob already suggested, you can also just create a rect and give it three different fills, e.g.
local rect = display.newRect( 200, 200, 300, 300 ) rect.frameA = { type = "image", filename = "image1.png" } rect.frameB = { type = "image", filename = "image2.png" } rect.frameC = { type = "image", filename = "image3.png" } rect.fill = rect.frameA
Thanks for the info, so to setup a sprite with three states, and only one frame per state is this sufficient?
local shipStates = { { name = "flyStraight", frames = { 4 } }, { name = "flyRight", frames = { 5 } }, { name = "flyLeft", frames = { 6 } } }
I guess my biggest question is, is this actually running an animation loop indefinitely just constantly updating the current frame to the same image? or by designating only single frame does the animation just update the image once until the state changes.
When you set up the sprite, you can choose if it loops once or loops indefinitely. Look at this sequence table setup:
local sfxSequenceData = { { name = "bluetail", start = 13, count = 5, time = 120, loopCount = 0 }, { name = "explosion", start = 41, count = 11, time = 600, loopCount = 1 }, }
“bluetail” is a flickering 5 frame exhaust coming out of the back of my player or enemy ships. With loopCount set to 0, it will play from frame 13 to frame 18 in the spite sheet endlessly.
“explosion” happens when something blows up. I only one to play the sequence once, so I set the loopCount to 1 and it will only go through the sequence once.
Now here is something else, that you can do if you don’t want to use any animation. Don’t use sprites, but use the imageSheet to feed display.newImageRect().
local droneShip = display.newImageRect(thisDrone, droneImageSheet, droneType, drones[droneType].width, drones[droneType].height) droneShip.x = 0 droneShip.y = 0
Now I have a table, that defines defines a lot about my drones, like the width and height of the image. If I want drone#3, droneType has the value of three and it looks up the data in my table. (The drone table contains a lot more information like speed, health, number of attacks, how fast they can fire etc.) But since I’m using a display.newImageRect() there is no animation other than me moving them using an EnterFrame listener based on move info in the droneTable. In the code above “thisDrone” is a display.newGroup() that holds the drone and its engine exhaust animation so I can move all of that at once.
Rob
Having animations that are 1 frame long are perfectly acceptable. Sprites give you various states that images can be in, such as “running”, “jumping” “moveleft”, “idle”, etc. Each of these states can have 1 or more images that make up the animation and it’s a convenient way to have your character show the left facing version, right facing version or idle version.
If not, you need to have three display.newImageRect()'s and hide two, show one, but move all three in unison. When you change state, you would hide two, show one etc. It can be done but sprites do a lot of that work for you.
Rob
In addition to what Rob already suggested, you can also just create a rect and give it three different fills, e.g.
local rect = display.newRect( 200, 200, 300, 300 ) rect.frameA = { type = "image", filename = "image1.png" } rect.frameB = { type = "image", filename = "image2.png" } rect.frameC = { type = "image", filename = "image3.png" } rect.fill = rect.frameA
Thanks for the info, so to setup a sprite with three states, and only one frame per state is this sufficient?
local shipStates = { { name = "flyStraight", frames = { 4 } }, { name = "flyRight", frames = { 5 } }, { name = "flyLeft", frames = { 6 } } }
I guess my biggest question is, is this actually running an animation loop indefinitely just constantly updating the current frame to the same image? or by designating only single frame does the animation just update the image once until the state changes.
When you set up the sprite, you can choose if it loops once or loops indefinitely. Look at this sequence table setup:
local sfxSequenceData = { { name = "bluetail", start = 13, count = 5, time = 120, loopCount = 0 }, { name = "explosion", start = 41, count = 11, time = 600, loopCount = 1 }, }
“bluetail” is a flickering 5 frame exhaust coming out of the back of my player or enemy ships. With loopCount set to 0, it will play from frame 13 to frame 18 in the spite sheet endlessly.
“explosion” happens when something blows up. I only one to play the sequence once, so I set the loopCount to 1 and it will only go through the sequence once.
Now here is something else, that you can do if you don’t want to use any animation. Don’t use sprites, but use the imageSheet to feed display.newImageRect().
local droneShip = display.newImageRect(thisDrone, droneImageSheet, droneType, drones[droneType].width, drones[droneType].height) droneShip.x = 0 droneShip.y = 0
Now I have a table, that defines defines a lot about my drones, like the width and height of the image. If I want drone#3, droneType has the value of three and it looks up the data in my table. (The drone table contains a lot more information like speed, health, number of attacks, how fast they can fire etc.) But since I’m using a display.newImageRect() there is no animation other than me moving them using an EnterFrame listener based on move info in the droneTable. In the code above “thisDrone” is a display.newGroup() that holds the drone and its engine exhaust animation so I can move all of that at once.
Rob