Advertisement

Sprite Sheet Need Help

Started by January 24, 2016 05:47 AM
10 comments, last by behdadsoft 8 years, 10 months ago
Hi.
I made this sprite sheet with Photoshop but what I Flip it in Game engine (AGK 2), seem animation move back during playing and don't play at current position. also I set my images to center of each sub-image.(between blue Lines). if possible please guide me this my spritesheet had problem or not?
Note: I used several Tools (Sprite sheet generator, Sprite sheet Creator, Texture Packer, ... but all had same result.)
Thanks.
Can you show us a code sample and/or a gif or video of what's going on?

I suspect you're flipping your sprite sheet instead of just flipping the rendered sprite.

// Pretend this is a sprite sheet with the following numbers drawn. (Original)
A B C
D E F

// If you flip the entire sprite sheet it will look like (Flipped)
C B A
F E D
So now when you tell it to draw the sprite at index 0, it prints out a reversed C. Instead, leave your sprite sheet alone, and tell the Original to draw sprite 0, but draw it flipped.

I don't use AGK, but a quick search didn't reveal a mechanism for drawing a flipped sprite, so you might need to map your own own frames to the Flipped texture.

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

Advertisement

Thanks for Reply.

This is a gif of my project.

https://www.dropbox.com/s/l7lri02ne2vgw5r/sample.gif?dl=0

also this is a part of my code:


Do

//Flip Player according to X Position

 if Player.x > Enemy.x //mean The player on the right side of the screen
    if PIsGround = 1 then SetSpriteFlip(Player.spr,1,0) 
 else
    if PIsGround = 1 then SetSpriteFlip(Player.spr,0,0)
 endif

 if GetPointerPressed() = 1
   PlayerFireBullet()
 endif

   //Move Bullet to Left and Right Direction
   for a = 1 to P_MaxBullet
       if GetSpriteExists(PBullet[a]) = 1
	  if Player.fire = 1			
	     SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0,GetSpriteYByOffset(PBullet[a]))
	  elseif Player.fire = -1
		 SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0,GetSpriteYByOffset(PBullet[a]))
	  endif
       endif
 next a

Loop


//=======================================Functions=======================================

//Create Player Fire Bullet Action (Attack 6)
Function PlayerFireBullet()
if Player.damage = 0
if Player.state = 0
            SetSpriteSize(player.spr,17.96875,-1)
            SetSpriteImage(player.spr,PlayerImg[12])
            SetSpriteAnimation(player.spr,230,201,5)
            PlaySprite(player.spr,10,0,1,5)
            Player.state = 11
endif
endif
EndFunction


Note: I used several Tools (Sprite sheet generator, Sprite sheet Creator, Texture Packer, ... but all had same result.)

Given this bit of information, emphasis added, I would say Eck is right and it is in your code somewhere. I don't know how it handles the sprite sheet, but if I am right, the Player.spr is the whole sprite sheet. I would suggest taking what you have, and when the player is on the opposite side (the right), move the sprite offset in the negative direction. This would make all your sprites face the proper way (as you already have) and transition (animate) in the order you desire.

yes, the Player.spr is the whole sprite sheet

and when the player is on the opposite side (the right), move the sprite offset in the negative direction.

No, I used below commands for get current Offset but Player Offset at (right or left) are same.


Print(GetSpriteOffsetX(Player.spr))
Print(GetSpriteOffsetY(Player.spr))

yes, the Player.spr is the whole sprite sheet

and when the player is on the opposite side (the right), move the sprite offset in the negative direction.


No, I used below commands for get current Offset but Player Offset at (right or left) are same.
Print(GetSpriteOffsetX(Player.spr))
Print(GetSpriteOffsetY(Player.spr))

What I mean is this: since you flipped the whole spritesheet, you must also flip the animation movement code. I will have to do a sprite mockup and source code sample for you when I get home tonight.
Advertisement

Alright, now I have a little time to do this.

Let's assume this is your sprite sheet, a simple 3 frame animation of a jump, animated from left to right:

[attachment=30435:SpriteSheet1.png]

You set your sprite to use the sprite sheet but only to display a single frame at a time:


SetSpriteSize(player.spr,17.96875,-1)
SetSpriteImage(player.spr,PlayerImg[12])

SetSpriteAnimation(player.spr,230,201,5)

And on each frame you are animating it:


if GetSpriteExists(PBullet[a]) = 1
     if Player.fire = 1            

     SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0,GetSpriteYByOffset(PBullet[a]))

     elseif Player.fire = -1

         SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0,GetSpriteYByOffset(PBullet[a]))

     endif

endif

Which moves your animation in this direction:

[attachment=30437:SpriteSheet3.png]

This seems to work. Everything goes well when the sprite is on the left. However, when you move to the opposite side, you do the following:


//Flip Player according to X Position


if Player.x > Enemy.x //mean The player on the right side of the screen

if PIsGround = 1 then SetSpriteFlip(Player.spr,1,0) 

else

if PIsGround = 1 then SetSpriteFlip(Player.spr,0,0)

endif

Which does this to the sprite sheet the sprite uses:

[attachment=30436:SpriteSheet2.png]

This flipped your image, making your sprite face the proper direction, however, you are still offsetting the sprite in the same direction since your left side/right side conditional doesn't include the movement/animation offset. What this means is your animation still follows the frames as indicated:

[attachment=30438:SpriteSheet4.png]

To fix this, you have two options.

Option 1 involves a left side/right side conditional check for the offset portion:


if GetSpriteExists(PBullet[a]) = 1
     if Player.fire = 1 
           if Player.x > Enemy.x
               SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0,GetSpriteYByOffset(PBullet[a])) // Inverted
          else

               SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0,GetSpriteYByOffset(PBullet[a])) // Original
           endif

     elseif Player.fire = -1
 

           if Player.x > Enemy.x
               SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0,GetSpriteYByOffset(PBullet[a]))  // Inverted
          else

               SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0,GetSpriteYByOffset(PBullet[a])) // Original
           endif

         

     endif

endif

Or something that takes into account the player's location.

Option 2 involves something a little more flexible and less code:

Add a player direction variable we use to make all adjustments to animations.


//Flip Player according to X Position
int PlayerDirection = 1 // Not sure what language, so however you declare an integer
if Player.x > Enemy.x //mean The player on the right side of the screen
PlayerDirection = -1

if PIsGround = 1 then SetSpriteFlip(Player.spr,1,0) 

else
PlayerDirection = 1

if PIsGround = 1 then SetSpriteFlip(Player.spr,0,0)

endif

Then use it.


if GetSpriteExists(PBullet[a]) = 1
     if Player.fire = 1            

     SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0*PlayerDirection,GetSpriteYByOffset(PBullet[a]))

     elseif Player.fire = -1

         SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0*PlayerDirection,GetSpriteYByOffset(PBullet[a]))
     endif

endif

With either option, you should have your sheet offsetting in the proper direction for the animation. In the case of the flipped sprite sheet:

[attachment=30439:SpriteSheet5.png]

Thanks Dragonsoulj for your help.

but this part of your code is relate to Bullet and not Player:


if GetSpriteExists(PBullet[a]) = 1
     if Player.fire = 1            

     SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0*PlayerDirection,GetSpriteYByOffset(PBullet[a]))

     elseif Player.fire = -1

         SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0*PlayerDirection,GetSpriteYByOffset(PBullet[a]))
     endif

endif

my player code is here:



Function PlayerFireBulletAnimation()
  if Player.damage = 0
    if Player.state = 0
      SetSpriteSize(player.spr,17.96875,-1)
      SetSpriteImage(player.spr,PlayerImg[12])
      SetSpriteAnimation(player.spr,230,201,5)
      PlaySprite(player.spr,10,0,1,5)
      Player.state = 11
  endif
    endif
EndFunction

//========================= Player Fire Bullet after Press Down + Right + A
	if GetRawKeyPressed(40) = 1 and P_StartTimer[4] = 0
		P_StartTimer[4] = 1
		PAttack[4] = 1
	endif
	
	//Start P_ActionTimer#
	if P_StartTimer[4] = 1
		if P_ActionTimer#[4] < 2.0 
			P_ActionTimer#[4] = P_ActionTimer#[4] + 0.05
		else
			P_ActionTimer#[4] = 0
			P_StartTimer[4] = 0
			PAttack[4] = 0
		endif
	endif
	
	if GetRawKeyPressed(PlayerReverseKey(39)) = 1 and PAttack[4] = 1 and P_ActionTimer#[4] < 2.0 then PAttack[4] = 2 //Right Key
	if GetRawKeyPressed(65) = 1 and PAttack[4] = 2 and P_ActionTimer#[4] < 2.0 then PAttack[4] = 3 //A Key

	if PAttack[4] = 3
		PlayerFireBulletAnimation()
	endif
	
	if Player.state = 11
		if GetSpriteCurrentFrame(Player.spr) = 5
			if PCreateBullet = 0
				CreatePlayerBullet()
				//check for bullet direction
				if Player.x < Enemy.x
					Player.fire = 1
				else
					Player.fire = -1
				endif				
				PCreateBullet = 1
			endif			
		endif	
	endif
	
	//Move Bullet to Left and Right Direction
	for a = 1 to P_MaxBullet
		if GetSpriteExists(PBullet[a]) = 1
			if Player.fire = 1			
				SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])+1.0,GetSpriteYByOffset(PBullet[a]))
			elseif Player.fire = -1
				SetSpritePositionByOffset(PBullet[a],GetSpriteXByOffset(PBullet[a])-1.0,GetSpriteYByOffset(PBullet[a]))
			endif
		endif
	next a
	
	//Delete Bullet when is outside of screen
	for a = 1 to P_MaxBullet
		if GetSpriteExists(PBullet[a]) = 1
			if GetSpriteX(PBullet[a]) > 110 or GetSpriteX(PBullet[a]) < -10 then DeleteSprite(PBullet[a])
		endif		
	next a

        // Back the Player to Idle after Finish Animation
	if Player.state = 11
		if GetSpritePlaying(Player.spr) = 0
			PCreateBullet = 0
			PAttack[4] = 0
			Player.state = 0
			PlayerIdle()
	endif
		endif	
//========================= END

We've pointed you in the right direction. It's time for you to try and solve your problem again. Read through our posts and take another crack at it.

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

We've pointed you in the right direction. It's time for you to try and solve your problem again. Read through our posts and take another crack at it.

I know and thanks you Dragonsoulj that tried to fix my problem. but seem he is misunderstood because SetSpritePositionByOffset() dose not change Player Offset but it change the player position (x,y) according to offset. and when you try to multiply GetSpriteXByOffset(PBullet[a])+1.0*PlayerDirection, in face you changed player move direction.(mean if with Press Right arrow key the player already moved to right, now it's move to left).

This topic is closed to new replies.

Advertisement