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

2018/3/13

How to use 2 Gif's in one Actor?

1
2
Ototo Ototo

2018/3/13

#
I have a primary Gif which should be always activated and i wanted to do another gif if I press "e" but unfortunately it wont work and the game starts freezing. Do someone have an idea? public class Spieler extends Actor { GifImage ImageGif = new GifImage("walknoweapons.gif"); // primary boolean attack = false; public void gifanimation() { GifImage ImageGif2 = new GifImage("character-horizontal-attack-moving.gif"); if (attack = true) { setImage(ImageGif2.getCurrentImage()); } else { setImage(ImageGif.getCurrentImage()); } } public void attack() { if (Staminabar.stamina > 0) { if (Greenfoot.isKeyDown("e")) { Staminabar.losestamina(); if (!spaceKeyIsDown) { attack = true; Actor Gegner1; Gegner1 = getOneObjectAtOffset(1,1,Gegner1.class); if (Gegner1 != null) { getWorld().removeObject(Gegner1); } Actor Gegner2; Gegner2 = getOneObjectAtOffset(1,1,Gegner2.class); if (Gegner2 != null) { getWorld().removeObject(Gegner2); } } } } } }
danpost danpost

2018/3/13

#
You will need a field to contain the GifImage object that is currently being used. You also need a field for the second GifImage object. You need an act method to determine which gif to run and to run it.
Ototo Ototo

2018/3/13

#
Can you give me an example please?
Ototo Ototo

2018/3/13

#
here is the entire Spieler code import greenfoot.*; public class Spieler extends Actor { GifImage ImageGif = new GifImage("walknoweapons.gif"); GreenfootSound blop = new GreenfootSound("Blop-Mark_DiAngelo-79054334.mp3"); GreenfootSound woosh = new GreenfootSound("Woosh-Mark_DiAngelo-4778593.mp3"); private int ySpeed; private int apexTimer; private int count = 0; private Healthbar healthbar; private Staminabar staminabar; boolean spaceKeyIsDown = false; boolean attack = false; public Spieler (Healthbar newHealhtbar) { this.healthbar = healthbar; } public Spieler (Staminabar newStaminabar) { this.staminabar = staminabar; } public Spieler() { } public void act() { gifanimation(); attack(); regain(); buying(); control(); jump(); fire(); hitboxGegner1(); hitboxStein(); hitboxHealing(); hitboxGoldhaufen(); kill(); } public void gifanimation() { GifImage ImageGif2 = new GifImage("character-horizontal-attack-moving.gif"); if (attack = true) { setImage(ImageGif2.getCurrentImage()); } else { setImage(ImageGif.getCurrentImage()); } } public void attack() { if (Staminabar.stamina > 0) { if (Greenfoot.isKeyDown("e")) { Staminabar.losestamina(); if (!spaceKeyIsDown) { attack = true; Actor Gegner1; Gegner1 = getOneObjectAtOffset(1,1,Gegner1.class); if (Gegner1 != null) { getWorld().removeObject(Gegner1); } Actor Gegner2; Gegner2 = getOneObjectAtOffset(1,1,Gegner2.class); if (Gegner2 != null) { getWorld().removeObject(Gegner2); } } } } } public void regain() { if (count == 500) { Staminabar.stamina = Staminabar.stamina +2; } if (count == 500) { count = 0; } else { count++; } } public void buying() { if (Goldhaufen.gold >= 20) { if (Greenfoot.isKeyDown("x")) { if (!spaceKeyIsDown) { Goldhaufen.gold = Goldhaufen.gold -20; WurfmesserAnzahl.axtanzahl = WurfmesserAnzahl.axtanzahl +1; } } else { spaceKeyIsDown=false; } } } public void control() { if (Greenfoot.isKeyDown("d")) { setLocation(getX()+3,getY()); } if (Greenfoot.isKeyDown("a")) { setLocation(getX()-2,getY()); } else { setLocation(getX()-1,getY()); } } public void jump() { int groundLevel = (710); boolean onGround = (getY() == groundLevel); if (!onGround) { if (ySpeed == 0 && apexTimer > 0) apexTimer--; if (ySpeed == 0 && apexTimer > 0) return; ySpeed++; setLocation(getX(), getY()+ySpeed); if (getY()>=groundLevel) { setLocation(getX(), groundLevel); Greenfoot.getKey(); } } else { if ("space".equals(Greenfoot.getKey())) { woosh.play(); ySpeed = -22; setLocation(getX(), getY()+ySpeed); apexTimer = 8; } } } public void fire() { if (WurfmesserAnzahl.axtanzahl >0) { if(Greenfoot.isKeyDown("f")) { if (!spaceKeyIsDown) { if (WurfmesserAnzahl.axtanzahl == 0) { spaceKeyIsDown = false; } else { WurfmesserAnzahl.axtanzahl = WurfmesserAnzahl.axtanzahl -1; getWorld().addObject(new Wurfmesser(), getX(), getY()); } } } else { spaceKeyIsDown = false; } } } public void hitboxStein() { Actor Stein; Stein = getOneObjectAtOffset(15,10,Stein.class); if (Stein != null) { Healthbar.losehealth2(); getWorld().removeObject(Stein); } } public void hitboxGegner1() { Actor Gegner1; Gegner1 = getOneObjectAtOffset(1,1,Gegner1.class); if (Gegner1 != null) { Healthbar.losehealth(); } } public void hitboxGegner2() { Actor Gegner2; Gegner2 = getOneObjectAtOffset(1,1,Gegner2.class); if (Gegner2 != null) { Healthbar.losehealth(); } } public void hitboxHealing() { Actor Healing; Healing = getOneIntersectingObject(Healing.class); if (Healing != null) { blop.play(); Healthbar.health = Healthbar.health +10; getWorld().removeObject(Healing); } } public void hitboxGoldhaufen() { Actor Goldhaufen; Goldhaufen = getOneIntersectingObject(Goldhaufen.class); if (Goldhaufen != null) { Gold.gold = Gold.gold+10; getWorld().removeObject(Goldhaufen); } } void kill() { if (Healthbar.health <= 0) { getWorld().addObject(new GameOver(),getWorld().getWidth()/2,getWorld().getHeight()/2); getWorld().removeObject(this); Greenfoot.stop(); Healthbar.health = Healthbar.health + 50; Staminabar.stamina = Staminabar.stamina + 20; Gold.gold = Gold.gold = 0; Score.score = Score.score = 0; } if (getX() == 0) { getWorld().addObject(new GameOver(),getWorld().getWidth()/2,getWorld().getHeight()/2); getWorld().removeObject(this); Greenfoot.stop(); } } }
danpost danpost

2018/3/13

#
You have this line as a variable:
GifImage ImageGif2 = new GifImage("character-horizontal-attack-moving.gif");
It needs to be a field. Move it outside the method. Then you need another field that you can assign one or the other GifImage object to (either ImageGif or ImageGif2) as the current animation. The boolean attack field would not really be needed then as you can check which gif is current for that info. Do not run the animation by way of ImageGif or ImageGif2, but run it by what is in the current animation field. An issue you most probably will run into is how to stop the attack animation. You will probably have to save the first frame of the animation in a field and constantly checck for the frame to appear again (after is has moved on to other frames). That would be when you restore the other animation back to being active.
danpost danpost

2018/3/13

#
My Animation Support Class scenario has code that has an actor switch off between different animations just as you would the GifImage objects in your scenario. Also, you may want to see my Animation vs. GifImage scenario which shows the differences between my Animation class and the GifImage class.
Ototo Ototo

2018/3/13

#
I'm sorry for asking, i'm not rly good at Greenfoot, I need to do this for my school project and in such huge codes like you've posted i understand literally nothing. Could you send me the code how exactly can i decide which one should run by now and how do i stop the other one from running so that it wont collide with each other? The 2 diffrent gifs have to be in separate fields so that the attack field switch by pressing "e" which gif is running by now and stop the current gif from running. Is this correct?
Ototo Ototo

2018/3/13

#
and how do i post the code like you did :'D
danpost danpost

2018/3/13

#
Ototo wrote...
and how do i post the code like you did :'D
Posting code? read this!
Ototo wrote...
The 2 diffrent gifs have to be in separate fields so that the attack field switch by pressing "e" which gif is running by now and stop the current gif from running. Is this correct?
The main reason for them to be in different fields is that you do not want to have the image files read every act cycle (and be stuck on the first image). So, that is for the fields that are to retain the two GifImage objects. The animation field will switch off as needed between the two objects and the act will run whatever animation is currently set to it. You would probably use something like (but maybe not exactly) this:
// initiate attack
if (Greenfoot.isKeyDown("e") && currentGif == ImageGif) // user requests attack and not yet attacking
{
    ImageGif2 = new GifImage("character-horizontal-attack-moving.gif"); // needs created on the fly or all images may not show  
    currentGif = ImageGif2; // this GifImage field needs added -- set attack animation
    stopAttackAtImage = getCurrentImage(); // get first frame image
    atStopImage = true; // this boolean field needs added -- used to detect return to first image
}
// run animation
setImage(getCurrentImage());
// detect end of attacck
if (currrentGif == ImageGif2 && atStopImage != getImage()) // if attacking and change in image involving first frame
{
    atStopImage = !atStopImage; // update indicator
    if (atStopImage) currentGif = ImageGif; // if change was to first image, resume normal animation
}
Ototo Ototo

2018/3/13

#
Thank you! I will try it.
danpost danpost

2018/3/13

#
Ototo wrote...
Thank you! I will try it.
I was about to edit -- I used the wrong conditions on line 12:
if (currentGif = ImageGif2 && atStopImage != (stopAttackAtImage == getImage()))
A GreenfootImage field, 'stopAttackAtImage', also needs added. The line says if attack animation is running and the value of the image detection field is not properly set (which means a change was made to the image of the actor either removing the first image or setting the first image), then the following code should update its value and possibly act on that new value change (if back to first image, resume normal animaiton).
Ototo Ototo

2018/3/14

#
With a field, you mean public void stopAttackAtImage () ? So a method ?
Ototo Ototo

2018/3/14

#
And what do i have to add in this field
danpost danpost

2018/3/14

#
Ototo wrote...
With a field, you mean public void stopAttackAtImage () ? So a method ?
No, not a method -- a field (a variable declared outside any method).
Ototo wrote...
And what do i have to add in this field
The only other thing you will need to do is assign ImageGif to currentGif in the constructor of the class.
Ototo Ototo

2018/3/14

#
Errors appear at : getCurrentImage(); getImage()
There are more replies on the next page.
1
2