BleepingComputer.com: Transform Sprite

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Transform Sprite Need someone to help how I can switch the way sprite looks..

#1 User is offline   danbrownlow 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 99
  • Joined: 05-March 07
  • Gender:Male

Posted 21 March 2008 - 07:05 AM

Hey there. I don't know if anyones read a book on Java Games Development by David Brakeen.

Well, here's where I need help. When I detect when the left and right key has been pressed I want to switch the sprite to look the corresponding way.

What I have tried so far is in my draw method, I use an if statement to check which way the sprite is travelling, then if the sprite is travelling left, I use a transform to set the way the sprite is looking.

This is what I have so far:

public void draw(Graphics2D g) {
		// draw background
		g.drawImage(bgImage, 0, 0, null);

		// draw sprite
		g.drawImage(player.getImage(),
			Math.round(player.getX()),
			Math.round(player.getY()),
			null);
		
		
		//If the player is facing travelling left, then switch the sprite around to face left.	
		transform = new AffineTransform();
		if (player.getVelocityX() < 0){
			transform.scale(-1,1);
			transform.translate(-player.getWidth(), 0);
			g.setTransform(transform);
			g.drawImage(player.getImage(), transform, null);
			
		}
		
	}


Needless to say this didn't work, when I tried running, when I pressed the left key, a new sprite appeared in the top corner and was looking the same way as before so it didn't even switch.

Any help would be great =]

Dan

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users