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

2018/5/31

I'm trying to add an object onto my World but it doesn't seem to work. Can someone tell me what I'm doing wrong

Comp Comp

2018/5/31

#
//In VikingLifeEater class  

  public void removeLife()
    {
        if( this.isTouching(Life.class) )
        {
            this.removeTouching(Life.class);//remove the Life class that the Life Eater is touching
            count++;// increment by 1 each life that was eaten
        }
    }

//In Viking class

    public void hit()
    {
        MyWorld world = (MyWorld) getWorld();
        if(this.isTouching(Sword.class) ) //checks whether Viking has been hit by a sword
        {    
            VikingLifeEater v = new VikingLifeEater();
            world.addObject(v, vikingX, 40); //position lifeEater to eat life
            vikingX += 50; //updates position to next Life
        }
    }
     private int vikingX = 40;
danpost danpost

2018/5/31

#
Comp wrote...
]I'm trying to add an object onto my World but it doesn't seem to work. Can someone tell me what I'm doing wrong
Show act method of Viking class. Also, show how count is declared in VikingLifeEater class.
Comp Comp

2018/5/31

#
//Viking Class
  
 /**
     * Act - do whatever the Viking wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        checkKeys();
        hit();
        death();
        axeDelayCount++;
    } 

//VikingLifeEater

 private int count; //Count the # of times Viking is hit
danpost danpost

2018/5/31

#
Okay, count will never be more than 1 for any VikingLifeEater object (each one eats one life). However, I do not see a need for the field as the number of those type objects in the world will give you the count. Do you have an act method in the VikingLifeEater class that calls removeLife?
Comp Comp

2018/5/31

#
/**
     * Act - do whatever the Axe wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        removeLife();
    }  
danpost danpost

2018/5/31

#
Comp wrote...
<< Code Omitted >>
Good there. So you are saying that the VikingLifeEater objects are not being placed into the world. Inside the if of the hit method, add the following like (temporarily -- as a check):
System.out.println("Viking touches Sword");
Run to when you think a VikingLifeEater object should be in the world and report any terminal output.
Comp Comp

2018/5/31

#
/**
     * Return a boolean value for conditional statement
     */
    public void hit()
    {
        MyWorld world = (MyWorld) getWorld();
        if(this.isTouching(Sword.class) ) //checks whether Viking has been hit by a sword
        {  
            System.out.println("Viking Touches Sword");
            VikingLifeEater v = new VikingLifeEater();
            world.addObject(v, vikingX, 40); //position lifeEater to eat life
            vikingX += 50; //updates position to next Life
        }
    }
Comp Comp

2018/5/31

#
No terminal window pops up
Comp Comp

2018/5/31

#
Correction. It doesn't work all the time
Comp Comp

2018/5/31

#
A terminal window popped up relaying the message but only a couple of times.
danpost danpost

2018/5/31

#
Comp wrote...
A terminal window popped up relaying the message but only a couple of times.
Every time it does work, a new VikingLifeEater object IS placed into your world and should by all rights remove a Life object from the world (provided you are placing them properly in the world).
You need to login to post a reply.