Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Transform Sprite


  • Please log in to reply
No replies to this topic

#1 Guest_danbrownlow_*

Guest_danbrownlow_*

  • Guests
  • OFFLINE
  •  

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

BC AdBot (Login to Remove)

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users