首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >项目帮助- Greenfoot/javascript

项目帮助- Greenfoot/javascript
EN

Stack Overflow用户
提问于 2016-05-20 22:52:20
回答 1查看 74关注 0票数 0

我有一个由一个子类定义的“生命条”,并在另一个子类中调用它,但由于某种原因,我和我的老师都无法让它更新……有什么原因吗?

下面是名为score的“生命条”方法:

代码语言: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”方法的类:

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-21 20:05:06

在MachoMan中,您可以创建一个Score对象,但不会将其添加到世界中。如果世界上有另一个Score对象,它不在同一个Score对象中,因此它不会更新。这里有几个解决方案:

  • 让MachoMan创建分数,但将其添加到世界中使用getWorld().addObject(...),在addedToWorld(World world)方法中
  • 使MachoMan获取分数,在addedToWorld(World world)方法中使用MachoMan <addedToWorld(World world)>H19>如果要在世界中创建MachoMan和分数,则可以首先创建分数并将其作为参数传递给MachoMan的MachoMan
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37349876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档