Custom Methods on Sprite

  ... the case you feel the need for a new feature or want to submit one.

Custom Methods on Sprite

Postby nebkat » Sun Jan 16, 2011 10:41 pm

Would it be possible to allow custom functions on a sprite? Here is an example:

  1. I make a sprite called door
  2. I add method called "openDoor" to the door sprite
  3. "openDoor" animates the door with a door animation
  4. Later on, I call "door.openDoor("open")" an that opens the door with an animation


This would be great for level based games because it would be easy to add interactivity to certain items. This would allow things like switches or buttons for lights. It could change

Code: Select all
open_the_gates()

to
Code: Select all
gate.open()



The only problem with this is that adding all of the methods would still be added manually in the game activity. To fix this, some kind of scripting code should be added to make andengine a bit more like a game engine. We could have mylevel.xml and mylevel.script. In our xml we can define a gate:

Code: Select all
<entity name="gate" width="200" height="300">


And in the script we just put

Code: Select all
Entity theGate = xml.getByName("gate");
theGate{
    function openGate(){
        this.animate(200);
    }
}


I know it is probably complicated, but with an addition like this, AndEngine would be way better and definately used in more games.
nebkat
 
Posts: 23
Joined: Thu Jan 06, 2011 5:46 pm

Re: Custom Methods on Sprite

Postby samyboy280 » Fri Jan 21, 2011 3:10 pm

nebkat wrote:Would it be possible to allow custom functions on a sprite? Here is an example:

  1. I make a sprite called door
  2. I add method called "openDoor" to the door sprite
  3. "openDoor" animates the door with a door animation
  4. Later on, I call "door.openDoor("open")" an that opens the door with an animation


This would be great for level based games because it would be easy to add interactivity to certain items. This would allow things like switches or buttons for lights. It could change

Code: Select all
open_the_gates()

to
Code: Select all
gate.open()



The only problem with this is that adding all of the methods would still be added manually in the game activity. To fix this, some kind of scripting code should be added to make andengine a bit more like a game engine. We could have mylevel.xml and mylevel.script. In our xml we can define a gate:

Code: Select all
<entity name="gate" width="200" height="300">


And in the script we just put

Code: Select all
Entity theGate = xml.getByName("gate");
theGate{
    function openGate(){
        this.animate(200);
    }
}


I know it is probably complicated, but with an addition like this, AndEngine would be way better and definately used in more games.


I have no idea what the heck you are talking about, just extend Sprite and add your own methods?
samyboy280
 
Posts: 49
Joined: Thu Jan 13, 2011 8:44 pm

Re: Custom Methods on Sprite

Postby AfroFire » Sat Mar 05, 2011 11:51 pm

Just in case it isn't clear what the word 'extends' means,
http://xahlee.org/java-a-day/extend.html

You basically want to replace the letter B in this tutorial with sprite, and letter C with something like "Gate", similar to:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class Gate extends Sprite
  2. {
  3.   Gate(x, y, textureThing)
  4.   {
  5.     super(x,y,textureThing);
  6.   }
  7.   public void openGate()
  8.   {
  9.     // do open stuff here.
  10.   }
  11. };
  12.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


Then your Gate class will inherit all of the sprite's properties and you can even add it to scenes like,


Gate gateA = new Gate(x, y, textureStuff);
sceneA.attachChild(gateA);

gateA.open();
AfroFire
 
Posts: 27
Joined: Thu Aug 19, 2010 1:57 am


Return to Features

Who is online

Users browsing this forum: No registered users and 9 guests