Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By JosAH

Thread: Player Entity w/ Top Down Movement: Switching out Animations

  1. #1
    NoMan's Avatar
    NoMan is offline Member
    Join Date
    Mar 2012
    Location
    Floriderp
    Posts
    5
    Rep Power
    0

    Default Player Entity w/ Top Down Movement: Switching out Animations

    Noob here.
    I followed the entity tutorial.

    And began toying around with the results.

    I made a walk loop SpriteSheet:

    and an idle SpriteSheet:


    And then I wrote an "AnimationRenderComponent" Class:
    Java Code:
    package engine.component.render;
    
    import org.newdawn.slick.Animation;
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.geom.Vector2f;
    import org.newdawn.slick.state.StateBasedGame;
    
    public class AnimationRenderComponent extends RenderComponent 
    {
    	 //RC subclass that renders an Animation, specifically
    		Animation ani;
    		Vector2f pos;
    		float rot;
    	 
    		public AnimationRenderComponent(String id, Animation ani)
    		{
    			super(id);
    			this.ani = ani;
    		}
    	 
    		@Override
    		public void render(GameContainer gc, StateBasedGame sb, Graphics g) 
    		{
    			pos = owner.getPosition();
    			rot = owner.getRotation();
    			g.rotate(pos.x + ani.getWidth() / 2, pos.y + ani.getHeight() / 2, rot);
    			ani.draw(pos.x, pos.y);
    			
    			
    		}
    	 
    		@Override
    		public void update(GameContainer gc, StateBasedGame sb, int delta) 
    		{
    			pos = owner.getPosition();
    			rot = owner.getRotation();
    			ani.update(4);
    		}
    	 
    	}
    This was in addition to the Entity, Component, RenderComponent, and ImageRenderComponent, and TopDownMovement classes I got from the tutorial.

    I also modified the TopDownMovement component:
    Java Code:
    package engine.component.movement;
    
    
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.geom.Vector2f;
    import org.newdawn.slick.state.StateBasedGame;
    
    import engine.component.Component;
    
    public class TopDownMovement extends Component {
    
    	float direction;
    	float speed;
    	boolean moving = false;
    
    	public TopDownMovement( String id )
    	{
    		this.id = id;
    	}
    
    	public float getSpeed()
    	{
    		return speed;
    	}
    
    	public float getDirection()
    	{
    		return direction;
    	}
    	
    	public boolean getMoving()
    	{
    		return this.moving;
    	}
    	
    	public void setMoving(boolean mov)
    	{
    		this.moving = mov;
    	}
    
    	@Override
    	public void update(GameContainer gc, StateBasedGame sb, int delta) {
    
    		float rotation = owner.getRotation();
    		float scale = owner.getScale();
    		Vector2f position = owner.getPosition();
    
    		Input input = gc.getInput();
    
           if(input.isKeyDown(Input.KEY_A))
           {
           	rotation += -0.2f * delta;
           }
    
           if(input.isKeyDown(Input.KEY_D))
           {
           	rotation += 0.2f * delta;
           }
    
           if(input.isKeyDown(Input.KEY_W))
           {
               float hip = 0.4f * delta;
    
               position.x += hip * java.lang.Math.sin(java.lang.Math.toRadians(rotation));
               position.y -= hip *java.lang.Math.cos(java.lang.Math.toRadians(rotation));
               this.setMoving(true);
           }
           else
           {
        	   this.setMoving(false);
           }
    
    		owner.setPosition(position);
    		owner.setRotation(rotation);
    		owner.setScale(scale);
    	}
    
    }
    I added a boolean variable called moving, and made its value dependent on the owner entity being moved forward by the 'W' key. I made a mutator and accessor method for moving.

    Then, in my Play state:
    Java Code:
    package game;
    
    import org.newdawn.slick.Animation;
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.SpriteSheet;
    import org.newdawn.slick.geom.Vector2f;
    import org.newdawn.slick.state.BasicGameState;
    import org.newdawn.slick.state.StateBasedGame;
    
    import engine.component.movement.TopDownMovement;
    import engine.component.render.AnimationRenderComponent;
    import engine.entity.Entity;
    
    public class Play extends BasicGameState
    {
    	
    	Entity player;
    	TopDownMovement playerMovement;
    	SpriteSheet walkLoop;
    	SpriteSheet idleLoop;
    	Animation walk;
    	Animation idle;
    	AnimationRenderComponent walkL;
    	AnimationRenderComponent idleL;
    	Vector2f position;
    	boolean mov;
    
    
    	public Play(int play) 
    	{
    
    	}
    
    	public void init(GameContainer gc, StateBasedGame sbg)
    			throws SlickException 
    	{
    		//initialize player entity and movement type. Add the movement type to the entity.
    		player = new Entity(null);
    		playerMovement = new TopDownMovement(null);
    		player.AddComponent(playerMovement);
    		//initialize the walk Animation, SpriteSheet, and ARC.
    		walkL = new AnimationRenderComponent("arc1", walk);
    		walkLoop = new SpriteSheet("res/spritesheets/WalkLoop.png", 88, 98);
    		walk = new Animation(walkLoop, 50);
    		//init. the idle Animation, SpriteSheet, and ARC. 
    		idleL = new AnimationRenderComponent("arc1", idle);
    		idleLoop = new SpriteSheet("res/spritesheets/IdleLoop.png", 88, 98);
    		idle = new Animation(idleLoop, 50);
    		//init. position.
    		position = new Vector2f(400, 300);
    		mov = playerMovement.getMoving();
    		
    		if(mov=true)
    		{
    			player.AddComponent((AnimationRenderComponent)walkL);
    			idle.stop();
    			walk.start();
    		}
    		else
    		{
    			player.AddComponent((AnimationRenderComponent)idleL);
    			walk.stop();
    			idle.start();
    		}
    		
    		
    	}
    
    	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
    			throws SlickException 
    	{
    
    		player.render(gc, sbg, g);
    		player.setPosition(position);
    		
    	}
    
    	public void update(GameContainer gc, StateBasedGame sbg, int delta)
    			throws SlickException 
    	{
    		player.update(gc, sbg, delta);
    	}
    
    	public int getID() 
    	{
    
    		return 1;
    	}
    
    }
    I created AnimationRenderComponents, Animations, and SpriteSheets, assembled them properly, and then initialized & added them onto a "player" entity along with a TopDownMovement component. I also initialized a variable dependent on the boolean from the TopDownMovement class.

    I am able to get the character to move around on the screen freely, but he just goes through the walk loop over and over again without ever stopping when I stop pressing 'W'.

    I can't seem to figure this out. Any help is appreciated.

    - above taken from identical thread on slick2d forums
    Last edited by NoMan; 08-09-2012 at 07:53 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by NoMan View Post
    Could you answer the question I've posted in this link, here?
    For most of us, the answer is no. If your question is important to you and you desire our help, then we appreciate your putting in the effort to make it easy and safe to answer it. Post the important information and code here without links. If the question isn't that important to you, then you can ignore this advice.
    DarrylBurke likes this.

  3. #3
    NoMan's Avatar
    NoMan is offline Member
    Join Date
    Mar 2012
    Location
    Floriderp
    Posts
    5
    Rep Power
    0

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by Fubarable View Post
    For most of us, the answer is no. If your question is important to you and you desire our help, then we appreciate your putting in the effort to make it easy and safe to answer it. Post the important information and code here without links. If the question isn't that important to you, then you can ignore this advice.
    Okay, I didn't think a link to the slick2d forums would be met with skepticism.
    (I imagine your browser shows where I'm linking you to when you rollover my link)

    I had my hopes up that I got a legitimate reply after spending all day on this...

    Well, let me just copy paste my whole post from over there, since you think I'm trying to make you do something unsafe (or something?)...

    EDIT:

    moved to first post
    Last edited by NoMan; 08-09-2012 at 07:06 AM.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by NoMan View Post
    Okay, I didn't think a link to the slick2d forums would be met with skepticism.
    (I imagine your browser shows where I'm linking you to when you rollover my link)

    I had my hopes up that I got a legitimate reply after spending all day on this...

    Well, let me just copy paste my whole post from over there, since you think I'm trying to make you do something unsafe (or something?)...
    I think that part of it is the ominous "clicky" that you used for the URL title. I understand that you aren't trying to scam anybody, but it might imply spamming to some people. I suggest that you change the title of the link that you provided to a more coherent description if you want more people to click on it.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    NoMan's Avatar
    NoMan is offline Member
    Join Date
    Mar 2012
    Location
    Floriderp
    Posts
    5
    Rep Power
    0

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by awinston View Post
    I think that part of it is the ominous "clicky" that you used for the URL title. I understand that you aren't trying to scam anybody, but it might imply spamming to some people. I suggest that you change the title of the link that you provided to a more coherent description if you want more people to click on it.
    Fair enough. I'll change it. I just wanted a condensed word so it wouldn't be a long and obnoxious url.

    Sorry if I sounded snippy or anything, I've just tried so many things to get this to work.


    Is there any chance that you have a solution to this problem of mine?
    Last edited by NoMan; 08-09-2012 at 07:04 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by awinston View Post
    I think that part of it is the ominous "clicky" that you used for the URL title.
    I don't see that as being important. Many file sharing sites are blocked by corporate firewalls; some are advertisement driven or otherwise waste one's bandwidth. To add my own views to Fubarable's statement, if the code is too long to post here it's too long for me to go through.

    I don't see that I'll be able to help with this question, as the code uses a host of classes which aren't part of the standard JDK. NoMan, to get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. If that's beyond you, then you need to find a forum for the org.newdawn classes you use in your code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    "made its value dependent on the owner entity being moved forward by the 'W' key"

    There's nothing up there that shows how this works. I'm presuming this is part of how slick 2d works, in which case (unless you're really lucky) I expect there's few (if any) people here who know this tech well enough.

    Anyway, that aside there's a couple of things you could do to try and narrow down the problem.
    I'd stick a load of debugging in your code...Sysout (or a proper logger), for example in the setMoving code, and any bits that use that flag. At the moment you have nothing of the sort and are essentially flying blind in trying to spot the bug.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by NoMan View Post
    I made a walk loop SpriteSheet:
    In almost six of the fifteen frames that guy has his left leg in front of him while only (at most) three frames show his right leg in front of him; that is a very silly walk.

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by JosAH View Post
    In almost six of the fifteen frames that guy has his left leg in front of him while only (at most) three frames show his right leg in front of him; that is a very silly walk.

    kind regards,

    Jos
    It's modelled on Mr Cleese.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by Tolls View Post
    It's modelled on Mr Cleese.
    Ah, yes; it's an extremely silly walk then (12487 points, back to Luton)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by JosAH View Post
    In almost six of the fifteen frames that guy has his left leg in front of him while only (at most) three frames show his right leg in front of him; that is a very silly walk.
    You mean that's a guy walking up the screen? I thought it was a gal walking down.

    db <-- more than a bit hypermetropic
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Player Entity w/ Top Down Movement: Switching out Animations

    Quote Originally Posted by DarrylBurke View Post
    You mean that's a guy walking up the screen? I thought it was a gal walking down.

    db <-- more than a bit hypermetropic
    You just have a dirty mind and that would be an utter utter silly walk (too sily for everybody else).

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Player Movement Help!
    By Mitch in forum Java Gaming
    Replies: 1
    Last Post: 04-13-2012, 06:51 AM
  2. Replies: 2
    Last Post: 03-10-2012, 05:26 AM
  3. animations with java?
    By shujisan87 in forum New To Java
    Replies: 3
    Last Post: 09-13-2011, 05:09 PM
  4. SWT Animations Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •