Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Do I need a SpriteObject?
Started by Xorcist Aug 20 2019 08:32 PM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Xorcist

[GLOBAL: userInfoPane.html]
Xorcist
  • Observer

  • 14 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,064 posts
  • Enterprise

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



[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 893 posts
  • Corona SDK

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

  • Michael Flad likes this

[TOPIC: post.html]
#4

Xorcist

[GLOBAL: userInfoPane.html]
Xorcist
  • Observer

  • 14 posts
  • Corona SDK

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.



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,064 posts
  • Enterprise

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




[topic_controls]
[/topic_controls]