Results 1 to 12 of 12
- 08-09-2012, 04:57 AM #1
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:
This was in addition to the Entity, Component, RenderComponent, and ImageRenderComponent, and TopDownMovement classes I got from the tutorial.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); } }
I also modified the TopDownMovement component:
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.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); } }
Then, in my Play state:
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.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 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 forumsLast edited by NoMan; 08-09-2012 at 06:53 AM.
-
Re: Player Entity w/ Top Down Movement: Switching out Animations
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.
- 08-09-2012, 05:42 AM #3
Re: Player Entity w/ Top Down Movement: Switching out Animations
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 postLast edited by NoMan; 08-09-2012 at 06:06 AM.
- 08-09-2012, 05:50 AM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Player Entity w/ Top Down Movement: Switching out Animations
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
- 08-09-2012, 05:54 AM #5
Re: Player Entity w/ Top Down Movement: Switching out Animations
Last edited by NoMan; 08-09-2012 at 06:04 AM.
- 08-09-2012, 09:13 AM #6
Re: Player Entity w/ Top Down Movement: Switching out Animations
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-09-2012, 09:50 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 08-09-2012, 10:37 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Player Entity w/ Top Down Movement: Switching out Animations
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-09-2012, 11:06 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 08-09-2012, 11:19 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2012, 01:35 PM #11
- 08-09-2012, 01:43 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Player Entity w/ Top Down Movement: Switching out Animations
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Player Movement Help!
By Mitch in forum Java GamingReplies: 1Last Post: 04-13-2012, 05:51 AM -
How to control gain between mouse movement and cursor movement ?
By DrPete in forum New To JavaReplies: 2Last Post: 03-10-2012, 04:26 AM -
animations with java?
By shujisan87 in forum New To JavaReplies: 3Last Post: 09-13-2011, 04:09 PM -
SWT Animations Example
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:23 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks