1

Im building a game using eclipse and now I wanted to use Cocos2d engine. My problem is I want to merge my done program and sliding menu of cocos2d engine. And now my question is how can I extend my Menu Class to Activity class so I can call onCreate and setContentView for my XML and to extend also to CCLayer Class to make my Menu Class connected on my Sliding Menu of Cocos2d.

thanks for any suggestions and help. And please apologize my question.

UPDATE: heres the code

public class Menu extends Activity implements OnClickListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        .... //some codes here
    }
    @Override
    public void onClick(View view) {
        // some codes here.
    }
}

and here's the method that I wanted to use from CCLayer but it needs to extend it from that class.

public static CCScene scene()
{
    CCScene scene = CCScene.node();
    CCLayer layer = new Menu();
    scene.addChild(layer);
    return scene;
}

1 Answers1

1

Can't extend 2 classes in Java. But you could use interfaces, or the observer / listener pattern.

mbmc
  • 5,024
  • 5
  • 25
  • 53
  • So is there a way so I can use their method from both classes? – user3691945 Aug 02 '14 at 14:55
  • If you extend `Activity`, you can use all its functions, and since your other class has static function, simply use `CCScene.scene()`. Or am i missing something? – mbmc Aug 02 '14 at 15:24
  • i have an error on the **new Menu()** it says that `type mismatch cannot convert Menu to CCLayer` – user3691945 Aug 02 '14 at 15:29
  • 1
    You can't assign an object `Menu` to a `CCLayer`, they aren't related. If you want to use `Menu` as a `CCLayer`, you should do something like: 1) create `public interface CCLayer`, and declare all the APIs (functions) that you need. 2) create `public class Menu extends Activity implements CCLayer`, and implement all the functions that were in the original `CCLayer` class. 3) now you can type `CCLayer menu = new Menu()` – mbmc Aug 04 '14 at 00:53
  • How do I declare API functions? what are those? is it like copying the whole CCLayer java class in CClayer interface class? – user3691945 Aug 04 '14 at 11:01
  • interface is similar to header (.h) in c++ – mbmc Aug 04 '14 at 14:24
  • im not familiar in c++ language though. – user3691945 Aug 04 '14 at 22:42
  • 1) `public interface myInterface { public void myFunction(); }` 2) `public class myClass implements myInterface { @Override public void myFunction() { System.out.println("myFunction"); } }` – mbmc Aug 04 '14 at 23:06