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

2016/1/14

Changing Image of Actor

ironphoenix20 ironphoenix20

2016/1/14

#
Here's my code (Sorry it's quite long):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level3Hero here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level3Hero extends Actor
{
    /**
     * Act - do whatever the Level3Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    int artifactCount= 0;
    int level3over= 0;
    //Hunter Variables
    int hc1= 0;
    int ths1= 0;
    int hs1= 0;
    int hc2= 0;
    int ths2= 0;
    int hf= 0;
    int hs2= 0;
    int hc3= 0;
    int ths3= 0;
    int hs3= 0;
    int hc4= 0;
    int ths4= 0;
    int hs4= 0;
    int hsend;
    //Bear Variables
    int bc1= 0;
    int tbs1= 0;
    int bf= 0;
    int bs1= 0;
    int bs2= 0;
    int bc2= 0;
    int tbs2= 0;
    int bs3= 0;
    int bc3= 0;
    int tbs3= 0;
    int bs4= 0;

    //Monkey Variables
    int ms1= 0;
    int mc1= 0;
    int tms1= 0;
    int ms2= 0;
    int mf= 0;
    int mc2= 0;
    int tms2= 0;
    int ms3= 0;
    public void act() 
    {
        if (Greenfoot.isKeyDown("left")) {
            move (-1);
        }
        if (Greenfoot.isKeyDown("right")) {
            move(1);
        }
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY()-1);
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY()+1);
        }

        if (Greenfoot.isKeyDown(" ")) {
            removeTouching(StartCard3.class);
        }

        //Hunter
        if (isTouching(Hunter.class)) {
            HunterCard1 hunter1= new HunterCard1();
            getWorld().addObject(hunter1, 350, 300);
            hc1++;
        }
        if (hc1==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard1.class)); 
            TrevorHunterSpeechBubble1 thspeech1= new TrevorHunterSpeechBubble1();
            getWorld().addObject(thspeech1, 388, 438);
            ths1= 1;;
        }   else if (hc1==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard1.class)); 
            TrevorHunterSpeechBubble1b thspeech1b= new TrevorHunterSpeechBubble1b();
            getWorld().addObject(thspeech1b, 388, 438);
            ths1= 2;;
        }   else if (hc1==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard1.class)); 
            TrevorHunterSpeechBubble1c thspeech1c= new TrevorHunterSpeechBubble1c();
            getWorld().addObject(thspeech1c, 388, 438);
            ths1= 3;;
        }

        if (ths1==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(Hunter.class));
            HunterSpeechBubble1 hspeech1= new HunterSpeechBubble1();
            getWorld().addObject(hspeech1, 107, 470);
            hs1= 1;
        }   else if (ths1==2 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(Hunter.class));
            HunterSpeechBubble1b hspeech1b= new HunterSpeechBubble1b();
            getWorld().addObject(hspeech1b, 107, 470);
            hs1= 2;
        }   else if (ths1==3 && Greenfoot.isKeyDown(" "))  {
            getWorld().removeObjects(getWorld().getObjects(Hunter.class));
            HunterSpeechBubble1c hspeech1c= new HunterSpeechBubble1c();
            getWorld().addObject(hspeech1c, 107, 470);
            hs1= 3;
        }

        if (hs1==1 && Greenfoot.isKeyDown(" ")) {
            HunterCard2 hunter2= new HunterCard2();
            getWorld().addObject(hunter2, 350, 300);
            hc2++;
        }   else if (hs1==2 && Greenfoot.isKeyDown(" ")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }   else if(hs1==3 && Greenfoot.isKeyDown(" ")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }

        if (hc2==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble1.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard2.class));
            TrevorHunterSpeechBubble2a thspeech2a= new TrevorHunterSpeechBubble2a();
            getWorld().addObject(thspeech2a, 388, 438);
            ths2= 1;
        }   else if (hc2==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble1.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard2.class));
            TrevorHunterSpeechBubble2 thspeech2= new TrevorHunterSpeechBubble2();
            getWorld().addObject(thspeech2, 388, 438);
            ths2= 2;
        }   else if (hc2==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble1.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard2.class));
            TrevorHunterSpeechBubble2c thspeech2c= new TrevorHunterSpeechBubble2c();
            getWorld().addObject(thspeech2c, 388, 438);
            ths2= 3;
        }

        if (ths2== 1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble1.class));
            HunterSpeechBubble2a hspeech2a= new HunterSpeechBubble2a();
            getWorld().addObject(hspeech2a, 107, 470);
            hs2= 1;
        }   else if (ths2== 2 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble1.class));
            HunterSpeechBubble2 hspeech2= new HunterSpeechBubble2();
            getWorld().addObject(hspeech2, 107, 470);
            hs2= 2;
        }   else if (ths2== 3 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble1.class));
            HunterSpeechBubble2c hspeech2c= new HunterSpeechBubble2c();
            getWorld().addObject(hspeech2c, 107, 470);
            hs2= 3;
        }

        if (hs2==1 && Greenfoot.isKeyDown(" ")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }   else if (hs2==2 && Greenfoot.isKeyDown(" ")) {
            HunterCard3 hunter3= new HunterCard3();
            getWorld().addObject(hunter3, 350, 300);
            hc3++;
        }  else if (hs2==3 && Greenfoot.isKeyDown(" ")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }

        if (hc3==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble2.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard3.class));
            TrevorHunterSpeechBubble3a thspeech3a= new TrevorHunterSpeechBubble3a();
            getWorld().addObject(thspeech3a, 388, 438);
            ths3= 1;
        }   else if (hc3==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble2.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard3.class));
            TrevorHunterSpeechBubble3b thspeech3b= new TrevorHunterSpeechBubble3b();
            getWorld().addObject(thspeech3b, 388, 438);
            ths3= 2;
        }   else if (hc3==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble2.class));
            getWorld().removeObjects(getWorld().getObjects(HunterCard3.class));
            TrevorHunterSpeechBubble3 thspeech3= new TrevorHunterSpeechBubble3();
            getWorld().addObject(thspeech3, 388, 438);
            ths3= 3;
        }

        if (ths3== 1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble2.class));
            HunterSpeechBubble3a hspeech3a= new HunterSpeechBubble3a();
            getWorld().addObject(hspeech3a, 107, 470);
            hs3= 1;
        }   else if (ths3== 2 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble2.class));
            HunterSpeechBubble3b hspeech3b= new HunterSpeechBubble3b();
            getWorld().addObject(hspeech3b, 107, 470);
            hs3= 2;
        }   else if (ths3== 3 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble2.class));
            HunterSpeechBubble3 hspeech3= new HunterSpeechBubble3();
            getWorld().addObject(hspeech3, 107, 470);
            hs3= 3;
        }

        if (hs3==1 && Greenfoot.isKeyDown(" ")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }   else if (hs3==2 && Greenfoot.isKeyDown(" ")) {
            HunterCard4 hunter4= new HunterCard4();
            getWorld().addObject(hunter4, 350, 300);
            hc4=2;
        }  else if (hs3==3 && Greenfoot.isKeyDown(" ")) {
            TrevorHunterSpeechBubbleEnd thspeechend= new TrevorHunterSpeechBubbleEnd();
            getWorld().addObject(thspeechend, 388, 438);
            hc4= 1;
        }

        if (hc4==1) {
            HunterSuccessCard hsc= new HunterSuccessCard();
            getWorld().addObject(hsc, 350, 300);
            artifactCount++;
        }

        if (hc4==2 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble2.class));
            HunterSpeechBubble3 hspeech3= new HunterSpeechBubble3();
            getWorld().addObject(hspeech3, 107, 470);
            hs4= 1;
        }   else if (hc4==2 && Greenfoot.isKeyDown("b")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }   else if (hc4==2 && Greenfoot.isKeyDown("c")) {
            HunterFailureCard hfc= new HunterFailureCard();
            getWorld().addObject(hfc, 350, 300);
            hf++;
        }

        if (hs4==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorHunterSpeechBubble3.class));
            TrevorHunterSpeechBubbleEnd thspeechend= new TrevorHunterSpeechBubbleEnd();
            getWorld().addObject(thspeechend, 388, 438);
            ths4= 1;            
        }

        if (ths4==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(HunterSpeechBubble3b.class));
            HunterSpeechBubble4 hspeech4= new HunterSpeechBubble4();
            getWorld().addObject(hspeech4, 107, 470);
            hsend= 1;
        }

        if (hsend==1 && Greenfoot.isKeyDown(" ")) {
            HunterSuccessCard hsc= new HunterSuccessCard();
            getWorld().addObject(hsc, 350, 300);
            artifactCount++;
        }

        //Bear
        if (isTouching(Bear.class)) {
            getWorld().removeObjects(getWorld().getObjects(Bear.class));
            BearSpeechBubble1 bspeech1= new BearSpeechBubble1();
            getWorld().addObject(bspeech1, 213, 289);
            bs1++;
        }

        if (bs1==1 && Greenfoot.isKeyDown(" ")) {
            BearCard1 bear1= new BearCard1();
            getWorld().addObject(bear1, 350, 300);
            bc1++;
        }

        if (bc1==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(BearCard1.class)); 
            TrevorBearSpeechBubble1a tbspeech1a= new TrevorBearSpeechBubble1a();
            getWorld().addObject(tbspeech1a, 388, 438);
            tbs1= 1;
        }   else if (bc1==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(BearCard1.class)); 
            TrevorBearSpeechBubble1b tbspeech1b= new TrevorBearSpeechBubble1b();
            getWorld().addObject(tbspeech1b, 388, 438);
            tbs1= 2;
        }   else if (bc1==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(BearCard1.class)); 
            TrevorBearSpeechBubble1 tbspeech1= new TrevorBearSpeechBubble1();
            getWorld().addObject(tbspeech1, 388, 438);
            tbs1= 3;
        }

        if (tbs1==1 && Greenfoot.isKeyDown(" ")) {
            BearFailureCard bfc= new BearFailureCard();
            getWorld().addObject(bfc, 350, 300);
            bf++;
        }   else if (tbs1==2 && Greenfoot.isKeyDown(" ")) {
            BearFailureCard bfc= new BearFailureCard();
            getWorld().addObject(bfc, 350, 300);
            bf++;
        }   else if (tbs1==3 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(BearSpeechBubble1.class));
            BearSpeechBubble2 bspeech2= new BearSpeechBubble2();
            getWorld().addObject(bspeech2, 213, 289);
            bs2++;
        }

        if (bs2==1 && Greenfoot.isKeyDown(" ")) {
            BearCard2 bear2= new BearCard2();
            getWorld().addObject(bear2, 350, 300);
            bc2++;        
        }

        if (bc2==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorBearSpeechBubble1.class));
            TrevorBearSpeechBubble2 tbspeech2= new TrevorBearSpeechBubble2();
            getWorld().addObject(tbspeech2, 388, 438);
            tbs2= 1;
        }   else if (bc2==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorBearSpeechBubble1.class));
            TrevorBearSpeechBubble2b tbspeech2b= new TrevorBearSpeechBubble2b();
            getWorld().addObject(tbspeech2b, 388, 438);
            tbs2= 2;
        }   else if (bc2==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorBearSpeechBubble1.class));
            TrevorBearSpeechBubble2c tbspeech2c= new TrevorBearSpeechBubble2c();
            getWorld().addObject(tbspeech2c, 388, 438);
            tbs2= 3;
        }

        if (tbs2==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(BearSpeechBubble2.class));
            BearSpeechBubble3 bspeech3= new BearSpeechBubble3();
            getWorld().addObject(bspeech3, 213, 289);
            bs3= 1;
        }   else if (tbs2==2 && Greenfoot.isKeyDown(" ")) {
            BearFailureCard bfc= new BearFailureCard();
            getWorld().addObject(bfc, 350, 300);
            bf++;
        }   else if (tbs2==3 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(BearSpeechBubble2.class));
            BearSpeechBubble3c bspeech3c= new BearSpeechBubble3c();
            getWorld().addObject(bspeech3c, 213, 289);
            bs3= 3;
        }

        if (bs3==1 && Greenfoot.isKeyDown(" ")) {
            BearCard3 bear3= new BearCard3();
            getWorld().addObject(bear3, 350, 300);
            bc3++;
        }   else if (bs3==3 && Greenfoot.isKeyDown(" ")) {
            BearFailureCard bfc= new BearFailureCard();
            getWorld().addObject(bfc, 350, 300);
            bf++;
        }

        if (bc3==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorBearSpeechBubble2.class));
            TrevorBearSpeechBubble3a tbspeech3a= new TrevorBearSpeechBubble3a();
            getWorld().addObject(tbspeech3a, 388, 438);
            tbs3= 1;
        } else if (bc3==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorBearSpeechBubble2.class));
            TrevorBearSpeechBubble3 tbspeech3= new TrevorBearSpeechBubble3();
            getWorld().addObject(tbspeech3, 388, 438);
            tbs3= 2;
        }   else if (bc3==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorBearSpeechBubble2.class));
            TrevorBearSpeechBubble3c tbspeech3c= new TrevorBearSpeechBubble3c();
            getWorld().addObject(tbspeech3c, 388, 438);
            tbs3= 3;
        }

        if (tbs3==1 &&Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(BearSpeechBubble3.class));
            BearSpeechBubble4 bspeech4= new BearSpeechBubble4();
            getWorld().addObject(bspeech4, 213, 289);
            bs4 ++;
        }   else if (tbs3==2 &&Greenfoot.isKeyDown(" ")) {
            BearFailureCard bfc= new BearFailureCard();
            getWorld().addObject(bfc, 350, 300);
            bf++;
        }   else if (tbs3==3 &&Greenfoot.isKeyDown(" ")) {
            BearFailureCard bfc= new BearFailureCard();
            getWorld().addObject(bfc, 350, 300);
            bf++;
        }

        if (bs4==1 && Greenfoot.isKeyDown(" ")) {
            BearSuccessCard bsc= new BearSuccessCard();
            getWorld().addObject(bsc, 350, 300);
            artifactCount++;
        }

        //Monkey
        if (isTouching(Monkey.class)) {
            getWorld().removeObjects(getWorld().getObjects(Monkey.class));
            MonkeySpeechBubble1 mspeech1= new MonkeySpeechBubble1();
            getWorld().addObject(mspeech1, 213, 289);
            ms1++;
        }

        if (ms1==1 && Greenfoot.isKeyDown(" ")) {
            MonkeyCard1 monkey1= new MonkeyCard1();
            getWorld().addObject(monkey1, 543, 174);
            mc1++;
        }

        if (mc1==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            TrevorMonkeySpeechBubble1 tmspeech1= new TrevorMonkeySpeechBubble1();
            getWorld().addObject(tmspeech1, 388, 438);
            tms1= 1;
        }   else if (mc1==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            TrevorMonkeySpeechBubble1b tmspeech1b= new TrevorMonkeySpeechBubble1b();
            getWorld().addObject(tmspeech1b, 388, 438);
            tms1= 2;
        }   else if (mc1==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            TrevorMonkeySpeechBubble1c tmspeech1c= new TrevorMonkeySpeechBubble1c();
            getWorld().addObject(tmspeech1c, 388, 438);
            tms1= 3;
        }

        if (tms1==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(MonkeySpeechBubble1.class));
            MonkeySpeechBubble2 mspeech2= new MonkeySpeechBubble2();
            getWorld().addObject (mspeech2, 213, 289);
            ms2++;
        }   else if (tms1==2 && Greenfoot.isKeyDown(" ")) {
            MonkeyFailureCard mfc= new MonkeyFailureCard();
            getWorld().addObject(mfc, 350, 300);
            mf++;
        }   else if(tms1==3 && Greenfoot.isKeyDown(" ")) {
            MonkeyFailureCard mfc= new MonkeyFailureCard();
            getWorld().addObject(mfc, 350, 300);
            mf++;
        }

        if (ms2==1 && Greenfoot.isKeyDown(" ")) {
            MonkeyCard2 monkey2= new MonkeyCard2();
            getWorld().addObject(monkey2, 350, 300);
            mc2++;
        }

        if (mc2==1 && Greenfoot.isKeyDown("a")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorMonkeySpeechBubble1.class));
            TrevorMonkeySpeechBubble2a tmspeech2a= new TrevorMonkeySpeechBubble2a();
            getWorld().addObject(tmspeech2a, 388, 438);
            tms2= 1;
        }   else if (mc2==1 && Greenfoot.isKeyDown("b")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorMonkeySpeechBubble1.class));
            TrevorMonkeySpeechBubble2 tmspeech2= new TrevorMonkeySpeechBubble2();
            getWorld().addObject(tmspeech2, 388, 438);
            tms2= 2;
        }   else if (mc2==1 && Greenfoot.isKeyDown("c")) {
            getWorld().removeObjects(getWorld().getObjects(TrevorMonkeySpeechBubble1.class));
            TrevorMonkeySpeechBubble2c tmspeech2c= new TrevorMonkeySpeechBubble2c();
            getWorld().addObject(tmspeech2c, 388, 438);
            tms2= 3;
        }

        if (tms2==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(MonkeySpeechBubble2.class));
            MonkeySpeechBubble2a mspeech2a= new MonkeySpeechBubble2a();
            getWorld().addObject(mspeech2a, 213, 289);
            ms3= 1;
        }   else if (tms2==2 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(MonkeySpeechBubble2.class));
            MonkeySpeechBubble3 mspeech3= new MonkeySpeechBubble3();
            getWorld().addObject(mspeech3, 213, 289);
            ms3= 2;
        }   else if (tms2==3 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(MonkeySpeechBubble2.class));
            MonkeySpeechBubble2c mspeech2c= new MonkeySpeechBubble2c();
            getWorld().addObject(mspeech2c, 213, 289);
            ms3=3;
        }

        if (ms3==1 && Greenfoot.isKeyDown(" ")) {
            getWorld().removeObjects(getWorld().getObjects(Monkey.class));
            MonkeySuccessCard msc= new MonkeySuccessCard();
            getWorld().addObject(msc, 350, 300);
            artifactCount++;
        }   else if (ms3==2 && Greenfoot.isKeyDown(" ")) {
            MonkeyFailureCard mfc= new MonkeyFailureCard();
            getWorld().addObject(mfc, 350, 300);
            mf++;
        }   else if (ms3==3 && Greenfoot.isKeyDown(" ")) {
            MonkeyFailureCard mfc= new MonkeyFailureCard();
            getWorld().addObject(mfc, 350, 300);
            mf++;
        }

        if (artifactCount==3) {
            getWorld().removeObjects(getWorld().getObjects(Level3Hero.class));
            getWorld().removeObjects(getWorld().getObjects(Hunter.class));
            getWorld().removeObjects(getWorld().getObjects(Bear.class));
            getWorld().removeObjects(getWorld().getObjects(Monkey.class));
            Level3OverCard over3= new Level3OverCard();
            getWorld().addObject(over3, 350, 300);
            level3over++;
        }

        if (level3over==0 && Greenfoot.isKeyDown(" ")) {
            Greenfoot.setWorld(new End());
        }
    }
}
For making the new hero with the speech bubbles, I'm currently removing the previous TrevorSpeechBubble object and adding a new one. But this is going to add the new object each time at the original coordinates of Trevor (Level3Hero) at the beginning of the level and I don't want this. Instead, I want to replace the image of the original Level3Hero object each time with my new image without creating a new object. How do I do this? Please kindly tell me if you happen to notice any other errors in my code as well.
davmac davmac

2016/1/14

#
I want to replace the image of the original Level3Hero object each time with my new image without creating a new object. How do I do this?
You can use the setImage method to set the image of an object (Is that really what you want?). See the documentation. Eg:
setImage("something.png");
But if you really just want to make the speech bubble appear at the current location of the Level3Hero object, use getX() and getY() to get its position and use these values when you call addObject. So for example:
            getWorld().addObject(mspeech3, getX(), getY());
You may want to apply an offset, for example:
            getWorld().addObject(mspeech3, getX() + 50, getY() - 40);
ironphoenix20 ironphoenix20

2016/1/14

#
Thank you very much but why do i need the offset so the speech bubble is a little above the original hero's image and not directly on it?
ironphoenix20 ironphoenix20

2016/1/14

#
Also, when I press run in greenfoot, my monkey and bear object disappear. Why does this happen?
ironphoenix20 ironphoenix20

2016/1/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;

/**
 * Write a description of class Hero here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hero extends Actor
{
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    int keyCount= 0;
    int cardCount= 0;
    public int riddleCard1= 0;
    public int riddleCard2= 0;
    public int riddleCard3= 0;
    public int riddleCard4= 0;
    int answerCard1= 0;
    int answerCard2= 0;
    int answerCard3= 0;
    int answerCard4= 0;
    int level2over= 0;

    public void act() 
    {
        if (Greenfoot.isKeyDown("left")) {
            move (-1);
            getWorld().removeObjects(getWorld().getObjects(StartCard1.class));
        }
        if (Greenfoot.isKeyDown("right")) {
            move(1);
            getWorld().removeObjects(getWorld().getObjects(StartCard1.class));
        }
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY()-1);
            getWorld().removeObjects(getWorld().getObjects(StartCard1.class));
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY()+1);
            getWorld().removeObjects(getWorld().getObjects(StartCard1.class));
        }

        if (isTouching(Monster.class)){
            setLocation (359,298);
            Greenfoot.setWorld(new Background());
            keyCount= 0;
        }

        //if (getX()== 300) {
        //GreenfootImage store = new GreenfootImage("Store.jpg");
        //store.scale(getWorld().getWidth(), getWorld().getHeight());
        //getWorld().setBackground(store);

        //Create loop
        if (isTouching(Key.class)) {
            keyCount++;
            removeTouching(Key.class);
        }

        if (getX()>180 && getX()<260 && getY()>380 && getY()<600 && keyCount==4) {
            Greenfoot.setWorld(new Background2());
            StartCard startCard= new StartCard();
            getWorld().addObject(startCard, 350, 300);
        }

        if (Greenfoot.isKeyDown("h")) {
            StartCard startCard= new StartCard();
            getWorld().addObject(startCard, 350, 300);
        }

        if (Greenfoot.isKeyDown("space")) {
            removeTouching(StartCard.class);
        }

        if (isTouching(Bat.class) && Greenfoot.isKeyDown("space")) {
            removeTouching(Bat.class);
        }   else if (isTouching(DoomBat.class) && Greenfoot.isKeyDown("space")) {
            Greenfoot.setWorld(new Background());
        }

        if (isTouching(Card.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card.class);
            riddleCard1= 1;
            RiddleCard1 riddle1= new RiddleCard1();
            getWorld().addObject(riddle1, 350, 300);
        }   else if (isTouching(Card2.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card2.class);
            riddleCard2= 1;
            RiddleCard2 riddle2= new RiddleCard2();
            getWorld().addObject(riddle2, 350, 300);
        }   else if (isTouching(Card3.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card3.class);
            riddleCard3= 1;
            RiddleCard3 riddle3= new RiddleCard3();
            getWorld().addObject(riddle3, 350, 300);
        }   else if (isTouching(Card4.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card4.class);
            riddleCard4= 1;
            RiddleCard4 riddle4= new RiddleCard4();
            getWorld().addObject(riddle4, 350, 300);
        }

        if (riddleCard1== 1 && Greenfoot.isKeyDown("c")) {
            removeTouching(RiddleCard1.class);
            answerCard1++;
            cardCount++;
            riddleCard1= 0;
            AnswerCard1 answer1= new AnswerCard1();
            getWorld().addObject(answer1, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 0; i<3; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat14= new Bat();
            getWorld().addObject(bat14, 436, 20);
            Bat bat15= new Bat();
            getWorld().addObject(bat15, 516, 20);
            Bat bat16= new Bat();
            getWorld().addObject(bat16, 39, 61);
        }   else if (riddleCard1== 1 && Greenfoot.isKeyDown("a")||
        riddleCard1== 1 && Greenfoot.isKeyDown("b") ||
        riddleCard1== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2()); 
        }

        if (riddleCard2== 1 && Greenfoot.isKeyDown("b")) {
            removeTouching (RiddleCard2.class);
            cardCount++;
            answerCard2++;
            riddleCard2=  0;
            AnswerCard2 answer2= new AnswerCard2();
            getWorld().addObject(answer2, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 3; i<6; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat17= new Bat();
            getWorld().addObject(bat17, 39, 184);
            Bat bat18= new Bat();
            getWorld().addObject(bat18, 39, 225);
            Bat bat19= new Bat();
            getWorld().addObject(bat19, 39, 347);
        }   else if (riddleCard2== 1 && Greenfoot.isKeyDown("a")||
        riddleCard2== 1 && Greenfoot.isKeyDown("c") ||
        riddleCard2== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2());
        }

        if (riddleCard3== 1 && Greenfoot.isKeyDown("a")) {
            removeTouching (RiddleCard3.class);
            cardCount++;
            answerCard3++;
            riddleCard3= 0;
            AnswerCard3 answer3= new AnswerCard3();
            getWorld().addObject(answer3, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 6; i<9; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat20= new Bat();
            getWorld().addObject(bat20, 39, 468);
            Bat bat21= new Bat();
            getWorld().addObject(bat21, 119, 581);
            Bat bat22= new Bat();
            getWorld().addObject(bat22, 438, 581);
        }   else if (riddleCard3== 1 && Greenfoot.isKeyDown("b")||
        riddleCard3== 1 && Greenfoot.isKeyDown("c") ||
        riddleCard3== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2());
        }

        if (riddleCard4== 1 && Greenfoot.isKeyDown("b")) {
            removeTouching (RiddleCard4.class);
            cardCount++;
            answerCard4++;
            riddleCard4= 0;
            AnswerCard4 answer4= new AnswerCard4();
            getWorld().addObject(answer4, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 9; i<12; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat23= new Bat();
            getWorld().addObject(bat23, 677, 581);
            Bat bat24= new Bat();
            getWorld().addObject(bat24, 663, 544);
            Bat bat25= new Bat();
            getWorld().addObject(bat25, 663, 383);
        }   else if (riddleCard4== 1 && Greenfoot.isKeyDown("a")||
        riddleCard4== 1 && Greenfoot.isKeyDown("c") ||
        riddleCard4== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2());
        }

        if (answerCard1!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard1.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard1.class));
            level2over++;
        }    

        if (answerCard2!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard2.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard2.class));
            level2over++;
        }    

        if (answerCard3!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard3.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard3.class));
            level2over++;
        }        

        if (answerCard4!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard4.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard4.class));
            level2over++;
        }     

        if (cardCount==4 && level2over==4) {  
            Greenfoot.setWorld(new Background3());
            StartCard3 startCard3= new StartCard3();
            getWorld().addObject(startCard3, 350, 300);
        }
    }
}
ironphoenix20 ironphoenix20

2016/1/14

#
This is my hero class for level 1 and 2. At line 228, I have the code to set world to level 3 but its not working. what's wrong with my code?
davmac davmac

2016/1/14

#
Thank you very much but why do i need the offset so the speech bubble is a little above the original hero's image and not directly on it?
I'm not sure I understand the question. If you place a speech bubble at the exact location of the actor, then it appears at the exact location of the actor. The line "getWorld().addObject(mspeech3, getX(), getY());" literally means "add the mspeech3 object at the exact location of this object". (Why would you expect it to place the speech bubble above the hero?) The offset has the effect of moving the new object some fixed distance and direction away from the current object.
Also, when I press run in greenfoot, my monkey and bear object disappear. Why does this happen?
I don't know. The code you posted above does not look like it should do that. Perhaps it is code in one of the other classes that is responsible.
davmac davmac

2016/1/14

#
At line 228, I have the code to set world to level 3 but its not working. what's wrong with my code?
What do you mean by "not working"? What happens? What is supposed to happen? The only problem I can see is that the StartCard3 object is added to the wrong world. You presumably wanted to add it to the new world, not the world that the hero is currently in (the current world). Lines 229-231 should be something like:
            World bg3 = new Background3();
            Greenfoot.setWorld(bg3);
            StartCard3 startCard3= new StartCard3();
            bg3.addObject(startCard3, 350, 300);
ironphoenix20 ironphoenix20

2016/1/14

#
The startcard3 is in the world. just the world isn't resetting to level 3's world.
davmac davmac

2016/1/14

#
Then either the code in those lines isn't actually executing (that is, the condition "cardCount==4 && level2over==4" may not be being met) or something is setting the world back again. I'm pretty sure the problem is not in the code you've posted (except for the problem I mentioned above).
You need to login to post a reply.