This site requires JavaScript, please enable it in your browser!
Greenfoot back
t-fire
t-fire wrote ...

2014/1/11

getImage()

t-fire t-fire

2014/1/11

#
Recently I've been having trouble using the getImage() method in my game. Here's what you need to know: I have a character in my game, that when he walks left, he faces left and when he walks right he faces right etc. What i want to know is how to make him punch either left or right depending on the image. Effectively i want: if image = "left.png" set image to "leftPunch.png" and same for the right side. if you do not know how to do this, if you can offer a simple example for the use of "getImage()", that would also be helpful. Thank you
Zamoht Zamoht

2014/1/11

#
You can't use the getImage() method to track the source file. What your can do is that you can declare a boolean that tells wether the player is turning left or right.
t-fire t-fire

2014/1/11

#
May I also point out that this game is not top down, it is side view, like a platformer
Gevater_Tod4711 Gevater_Tod4711

2014/1/11

#
Like Zamoht already said it's not possible to track the source file of the image. But if you keep a reference to the image in your class you can check which image is the current image of your actor:
1
2
3
4
5
6
7
8
9
10
11
//in your actor class you have to save the reference to the image:
private GreenfootImage image1 = new GreenfootImage("path1.png");
private GreenfootImage image2 = new GreenfootImage("path2.png");
 
//now you can check whether your actor is looking right or left like this (lets say image1 is left and image2 is right):
if (getImage().equals(image1)) {
    //your actor is looking to the left;
}
else if (getImage().equals(image2)) {
    //your actor is looking to the right;
}
danpost danpost

2014/1/11

#
What Gevater_Tod4711 said is true -- provided that the image was initially set to 'image1' or 'image2' in the construction of the class object. If not, the actor would have to move to activate the proper punching or kicking.
t-fire t-fire

2014/1/11

#
Thank's, I think you've cracked it!
t-fire t-fire

2014/1/12

#
Something doesn't seem to be working, but that may be my incorporation of it into the game
t-fire t-fire

2014/1/12

#
This may be a bit long, but I'm sure you'll find the bits you need: private int speed = 7; private int vSpeed = 0; private int acceleration = 1; private int jumpStrength = -16; private int healthr = 10; public int hitWait = 0; private GreenfootImage image1 = new GreenfootImage("TomRsideReverse.png"); private GreenfootImage image2 = new GreenfootImage("TomRside.png"); public void act() { checkKeys(); checkFall(); image(); deadr(); checkForWin(); } /** * If health for whatever reason, needs to be raised. */ public void setHealthr(int points) { healthr += points; } /** * What is my health? */ public int getHealthr() { return healthr; } /** * If player 2 no longer can be found in the world, we have won! */ public void checkForWin() { if(hitWait >= 1) { if(getWorld().getObjects(TomH.class).isEmpty()) { TomRwin tomRwin = new TomRwin(); getWorld().addObject(tomRwin, 332, 236); } } } /** * If I've run out of health, remove me from this world. */ public void deadr() { if(healthr <= 0) { getWorld().removeObject(this); } } /** * Punch TomH (player 2) if in range. */ public void hitTomH() { //is he next to me? TomH tomh = (TomH) getOneObjectAtOffset(0, 0, TomH.class); if (tomh != null) { tomh.setHealthh(-1);//decrements the health of this enemy; } } /** * Keyboard control and punching. */ private void checkKeys() { if(Greenfoot.isKeyDown("left")) { setImage("TomRsideReverse.png"); moveLeft(); } if(Greenfoot.isKeyDown("right")) { setImage("TomRside.png"); moveRight(); } if(Greenfoot.isKeyDown("/")) { //punch if you didn't in the last act cycle if(hitWait <= 0) { //Is image facing left or right? if(getImage().equals(image1)) { setImage("TomRsideReversePunch.png"); } else if(getImage().equals(image2)) { setImage("TomRsidePunch.png"); } hitTomH(); hitWait ++; } } else { if(hitWait >= 1) { //charge up punch hitWait --; } } //if you are on the ground, jump if the "up" key is pressed (platform counts as ground) if(onGround()) { if(Greenfoot.isKeyDown("up")) { jump(); } } }
Zamoht Zamoht

2014/1/12

#
Instead of:
1
2
3
4
5
6
7
8
9
10
if(Greenfoot.isKeyDown("left"))
{
    setImage("TomRsideReverse.png");
    moveLeft();
}
if(Greenfoot.isKeyDown("right"))
{
    setImage("TomRside.png");
    moveRight();
}
You have to use the images you have already declared.
1
2
3
4
5
6
7
8
9
10
if(Greenfoot.isKeyDown("left"))
{
    setImage(image1);
    moveLeft();
}
if(Greenfoot.isKeyDown("right"))
{
    setImage(image2);
    moveRight();
}
This should work. (someone please correct me if I'm wrong)
t-fire t-fire

2014/1/12

#
That should! Thanks again
t-fire t-fire

2014/1/12

#
It works!
You need to login to post a reply.