2011-05-29 52 views
0

我運行這段代碼:方法論證問題

graphic.addAnimation("standart",new int[] {0,1},1.0,true); 

至極調用graphic.addAnimation(字符串,INT [],浮點,boolean)方法:

public void addAnimation(String nme,int[] frmes,float time,boolean loop) { 
     animations.push(new Aim(nme, frmes, time, loop)); 
    } 

,但我得到這個錯誤:

the function addAnimation(String,int[],float,boolean) does not exist. 

SpriteSheet:

package progame; 

import java.util.Stack; 

import processing.core.PImage; 

public class SpriteSheet extends Graphic { 
    public int height,width,index; 
    public int timer; 
    public String playing; 
    public Stack<PImage> sheet = new Stack<PImage>(); 
    public Stack<Aim> animations = new Stack<Aim>(); 

    public SpriteSheet(String path,int h,int w) { 
     super(path); 
     height = h; 
     width = w; 
     for(int i = 0; i < Math.floor(source.height/height); i++) { 
      for(int j = 0; j < Math.floor(source.width/width); j++) { 
       sheet.push(source.get(j*width, i*height, width, height)); 
      } 
     } 
    } 

    public SpriteSheet(String path,Entity e,int h,int w) { 
     super(path,e); 
     height = h; 
     width = w; 
     for(int i = 0; i < Math.floor(source.height/height); i++) { 
      for(int j = 0; j < Math.floor(source.width/width); j++) { 
       sheet.push(source.get(j*width, i*height, width, height)); 
      } 
     } 
    } 

    public Aim getAnimation(String name) { 
     for(int i = 0; i< animations.size(); i++) 
     { 
      if(animations.get(i).name == name) { 
       return(animations.get(i)); 
      } 
     } 
     return null; 
    } 

    public void play(String name) { 
     for(int i = 0; i< animations.size(); i++) 
     { 
      if(animations.get(i).name == name) { 
       playing = name; 
       index = 0; 
       timer = 0; 
      }else 
      { 
       playing = null; 
       return; 
      } 
     } 
    } 

    public void update() { 
     timer ++; 
     Aim aim = getAnimation(playing); 
     if(timer > aim.frameRate) 
     { 
      timer = 0; 
      if(index == aim.frames.length) 
      { 
       if(aim.looping) { 
        index = 0; 
       }else 
       { 
        playing = null; 
       } 
      }else 
      { 
       index++; 
      } 
     } 
     source = sheet.get(index); 
    } 

    public void render() { 
     update(); 
     super.render(); 
    } 

    public void addAnimation(String nme,int[] frmes,float time,boolean loop) { 
     animations.push(new Aim(nme, frmes, time, loop)); 
    } 

    private class Aim 
    { 
     String name; 
     int[] frames; 
     float frameRate; 
     boolean looping; 
     public Aim(String nme,int[] frmes,float time,boolean loop) 
     { 
      name = nme; 
      frames = frmes; 
      frameRate = time; 
      looping = loop; 
     } 
    } 
} 
+0

發佈'addAnimation(...)'是其成員的類的聲明。還發布'graphic'變量的聲明。 – ssapkota 2011-05-29 09:37:26

+0

值'1.0'是一個double而不是一個float,所以我不相信你完全符合你描述的情況。您需要再次檢查您的詳細信息,以確切地查看問題所在。 – 2011-05-29 09:37:59

+0

我從來沒有聽說過Java調用任何「函數」。 – johusman 2011-05-29 09:38:54

回答

0

你說

its declared in the Entity class, like this: public Graphic graphic;

聲明爲:基於OP的評論

public SpriteSheet graphic; 
+0

嗯,我只是認爲,如果你宣佈一個變量作爲超類,它將能夠被定義爲它的一個孩子。 – TheBreadCat 2011-05-29 10:28:35

1

你從哪裏獲得下列行中的「圖形」實例?

graphic.addAnimation("standart",new int[] {0,1},1.0,true);

或者更重要的是,什麼是它的聲明?您不能在Graphic類型的變量上致電addAnimation。因爲它定義了這種方法的SpriteSheet

+0

其在Entity類中聲明,如下所示:'public Graphic graphic;' – TheBreadCat 2011-05-29 10:13:51

+0

@TheBreadCat - 而'Graphic'類沒有'addAnimation'方法? – 2011-05-29 10:15:05

+0

否,但是因爲它定義爲圖形變量應該具有的SpriteSheet(圖形擴展)。 – TheBreadCat 2011-05-29 10:19:39

1

its declared in the Entity class, like this: public Graphic graphic;

((SpriteSheet)graphic).addAnimation("standart",new int[] {0,1},1.0,true);

會解決這個問題。