2016-03-08 128 views
1

我一個簡單的馬里奧側滾動工作方式滾動(),當我嘗試調用滾動對象滾動()方法,我得到無法找到符號 - 方法滾動( )錯誤拋出。請幫忙!
這裏是Subworld文件找不到符號 -

import greenfoot.*; 
import java.util.ArrayList; 
/** 
* Write a description of class MarioWorld here. 
* 
* @author (your name) 
* @version (a version number or a 


public class MarioWorld extends World 
{ 
    ArrayList<MovingActor> moving = new ArrayList<MovingActor>(); 
    ArrayList<Actor> things = new ArrayList<Actor>(); 
    Message messagebox = new Message(""); 
    /** 
    * Constructor for objects of class MarioWorld. 
    * 
    */ 
    public MarioWorld() 
    { 
     super(800, 600, 1); 
     for(int i = 0; i < 40; i++) 
      { 
       things.add(new GreenApple()); 
       things.add(new Shamrock()); 

      } 
      for(int i = 0; i<8;i++) 
       things.add(new Brick()); 
      moving.add(new Mario()); 
      for(int r = 0;r < things.size();r++) 
      { 
       addObject(things.get(r),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(600)); 
      } 
    } 
    public void act() 
    { 

     for(int i = 0;i < things.size();i++) 
      { 
       things.get(i).scroll(); 
      } 
      addObject(moving.get(0),15,300); 
      moving.get(0).worldact(); 
    } 


} 
` 

這裏是3個滾動類別之一(它們是除名稱/圖片相同

import greenfoot.*; 
public class Shamrock extends Actor implements Scrollable 
{ 
    /** 
    * Act - do whatever the Shamrock wants to do. This method is called whenever 
    * the 'Act' or 'Run' button gets pressed in the environment. 
    */ 

    public Actor worldact() 
    { 
     scroll(); 
     return this; 
    } 
    public void scroll() 
    { 
     int x = getX(); 
     int y = getY(); 
     if(x <= 0) 
      x = 800; 
     x -= 6; 
     setLocation(x,y); 

    } 
} 

這裏是 '可滾動' 接口

import greenfoot.*; 

public interface Scrollable 
{ 
public void scroll(); 
} 
+2

我不打算回答,但我想你可能要明確施放,例如'((Shamrock)things.get(i)).roll();' –

+0

是演員一類實現可滾動? –

回答

0

這裏movingArrayList不是Shamrock一個object,所以你只能撥打arraylist類方法與moving對象像add()remove()

這裏的ArrayList is generic type Safed的ArrayList . So, you need to remove the對象from the arraylist then only try to apply method滾動()`

使用此代碼

((Shamrock)things.get(i)).scroll(); 

,而不是things.get(i).scroll();

0

thingsActor的列表。 Actor不執行Scollable,但Shamrock呢。 things.get(i)返回Actor其中沒有Scollable.scroll()。您需要將它投射到ShamrockScollable。或者你可以使things列表Shamrock