2016-05-20 3 views
0

У меня есть «бар жизни», определенный одним подклассом, и называйте его другим, но почему-то мой учитель и я не могли его обновить ... по какой-либо причине?Справка по проекту - Greenfoot/javascript

Вот метод «жизнь бар», названный оценка:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 
import java.awt.*; 
/** 
* Write a description of class Score here. 
* 
* @author James Brown 
* @version 1.0 
*/ 
public class Score extends Actor 
{ 
    Font font = new Font("Dialog", Font.BOLD, 20); 
    Color darkGreen = new Color(255, 51, 0); 
    Color green = new Color(255, 0, 0, 150); 
    GreenfootImage image = new GreenfootImage(100,30); 
    private int score = 3; 
    /** 
    * Score - sets up the score object 
    */ 
    public Score() 
    { 
     image.setFont(font); 
     setText(); 
     setImage(image); 
    } 
    /** 
    * setText - sets the text of the score 
    */ 
    private void setText() 
    { 
     image.clear(); 
     image.setColor(green); 

     image.drawString("Life:" + score, ShiftSouth(1,2), ShiftEast(15,2)); 
     image.setColor(darkGreen); 

     image.drawString("Life:" + score, 1, 15); 
    } 
    /** 
    * updateScore - adds score then runs setText 
    */ 
    public void updateScore() 
    { 
     score--; 
     setText(); 
     setImage(image); 
    } 
    /** 
    * ShiftSouth - shifts the coordinates down by the distance handed to it 
    * @param int p 
    * @param int distance 
    */ 
    public int ShiftSouth(int p, int distance){ 
     return(p+distance); 
    } 
    /** 
    * ShiftEast - shifts the coordinates right by the distance handed to it 
    * @param int p 
    * @param int distance 
    */ 
    public int ShiftEast(int p, int distance){ 
     return(p+distance); 
    } 
    public void setSpeed() 
    { 
     if(score>20) 
     { 
      Greenfoot.setSpeed(30); 
     } 
    }  
} 

и здесь класс пытается назвать некоторые из «lifebar в» методы:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 

/** 
* Write a description of class MachoMan here. 
* 
* @author (your name) 
* @version (a version number or a date) 
*/ 
public class MachoMan extends Actor 
{ 
    public int life = 3; 
    Score score = new Score(); 

    /** 
    * Act - do whatever the MachoMan wants to do. This method is called whenever 
    * the 'Act' or 'Run' button gets pressed in the environment. 
    */ 
    public void act() 
    { 
     moveAround(); 
     eat(); 
     eatHulk(); 
    } 
    public void moveAround() 
    { 

     move(2); 
     if (Greenfoot.getRandomNumber(100) <10) 
     { 
      turn(Greenfoot.getRandomNumber(90) -45); 
     } 
     if (getX() <=5 || getX() >= getWorld().getWidth()-5) 
     { 
      turn(180); 
     } 
     if (getY() <=5 || getY() >= getWorld().getHeight()-5) 
     { 
      turn(180); 
     } 

    }  
    public void eat() 
    { 
     Actor belt; 
     belt = getOneObjectAtOffset(0, 0, Belt.class); 
     if(belt != null) 
     { 
      World world; 
      world= getWorld(); 
      world.removeObject(belt); 
      world.addObject(belt,Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400)); 
      Greenfoot.playSound("Macho.wav"); 
      life = life - 1; 
      score.updateScore(); 
     } 

    } 
    public void eatHulk() 
    { 
     Actor hulk; 
     hulk = getOneObjectAtOffset(0, 0, Hulkamania.class); 
     if(hulk != null && life < 1) 
     { 
      World world; 
      world = getWorld(); 
      world.removeObject(hulk); 
      Greenfoot.playSound("Macho.wav"); 
      world.addObject(new GameOver(),300, 200); 
      Greenfoot.stop(); 
     } 
    } 
} 

ответ

0

В MachoMan вы создаете объект Score, но вы не добавляете его в мир. Если у вас есть другой объект Score в мире, он не в одном и том же, поэтому он не будет обновляться. Вот несколько решений:

  • Пусть MachoMan создать результат, но добавить его к миру с помощью getWorld().addObject(...), в addedToWorld(World world) методе
  • Сделать MachoMan принести результат, используя getWorld().getObjects(Score.class).get(0) в addedToWorld(World world) методе
  • Если вы создают MachoMan и Score в мире, тогда вы можете сначала создать оценку и передать параметр Score в качестве параметра для конструктора MachoMan.