传递包含两个整数或不同方法的对象?

时间:2015-02-27 17:16:48

标签: java android cocos2d-iphone

我有这种方法。需要一个节点,我需要它采取两个整数。

   public void resetAnchorpoints(Object sender, Object passed){
        Log.v("TAG", "CALLED FUNCTION 1!" + sender.toString());
        Log.v("TAG", "The center tile was " + (int[]) passed[1] + "," + (int[]) passed[2]);
    }

要调用它,我使用CCCallFuncND mresetAnchorpoint = CCCallFuncND.action(this, "resetAnchorpoints", pass);

当我收集时,CCCallFuncND传递一个节点(N)和一个数据对象(D)。但是,如果我像这样编写数据对象:

int[] pass = new int[3]; pass[0] = cC; pass[1] = cR;

它不起作用(因为它需要是一个对象?)。所以我的问题是:如何在一个对象中“包装”两个int-s,并在我的resetAnchorpoints方法中使用它们?

//编辑;忘了提错。错误是:“02-27 18:54:22.898 4925-4961 / nl.happyworx.squareone W / System.err:java.lang.NoSuchMethodException:resetAnchorpoints [class java.lang.Object,class java.lang.Object] “

感谢。

2 个答案:

答案 0 :(得分:0)

使用ArrayList

List<Integer> list = new ArrayList<Integer>();
list.add(cc);
list.add(cR);

答案 1 :(得分:0)

问题出在演员表上,请看这个例子:

Object intArray = new int[] { 1 };
System.out.println((int[])intArray[0]); //fails
System.out.println(((int[])intArray)[0]); //works

所以将代码更改为:

Log.v("TAG", "The center tile was " + ((int[]) passed)[0] + "," + ((int[]) passed)[1]);

正如评论中所指出的,你也可以在开始时把它投出来:

passed = (int[])passed;
Log.v("TAG", "The center tile was " + passed[0] + "," + passed[1]);