Hi Kevin. I suggest using the subclassing method of event handling,
but I'll talk about that in a bit.
If you do use registerEventForDelegation, the first parameter is the
component, typically "this", meaning your activity (form) is the
object that will handle the event. The second parameter is just for
logging, the third is important and is the event name, e.g., "Touched"
for a canvas, "Click" for a button, "EdgeReached" for an image sprite
edge reached event.
You'll deal with the event in a dispatchEvent method. In that, you can
ask the component of the event and also the event name, e.g.,
public boolean dispatchEvent(Component component, String id, String
eventName, Object[] args) {
if (component.equals(this) && eventName.equals("Initialize"))
*** now using the easier to use subclassing ***
Here the idea is not have to worry about delegation and registering,
etc. Just subclass the component classes and override the "event
handling" methods you want, e.g., ImageSprite.EdgeReached.
This works fine, though we were having problems with the CollidedWith.
Hossein fixed it-- you just have to call the super.CollidedWith at the
top and you don't get the stack overflow. Anyway, here's sample
code...Note that I do use the event registering to do the "Initialize"
method for the Activity itself. Apparently, you should only create
objects in $define, and set their parameter in Initialize. Here it
is...
package com.javabridge.samples;
import com.google.devtools.simple.runtime.components.Component;
import
com.google.devtools.simple.runtime.components.HandlesEventDispatching;
import com.google.devtools.simple.runtime.components.android.*;
import com.google.devtools.simple.runtime.events.EventDispatcher;
public class BallAndImageSpriteActivity extends Form {
Canvas canvas;
GameBall ball;
GameImageSprite imageSprite;
void $define() {
canvas = new Canvas(this);
imageSprite = new GameImageSprite(canvas);
ball = new GameBall(canvas);
EventDispatcher.registerEventForDelegation(this, "test",
"Initialize");
}
//@Override
//public void Initialize() {
// TODO Auto-generated method stub
// super.Initialize();
//initData();
//}
@Override
public boolean dispatchEvent(Component component, String id,
String eventName, Object[] args) {
if (component.equals(this) && eventName.equals("Initialize"))
{
initData();
return true;
}
return false;
}
/** * Method called on startup to do caching, initialization.
We need to do this so that
* AI initializes all sounds, otherwise, it will run into issue
switching between sounds.
*/
private void initData() {
canvas.Height(this.Height());
canvas.Width(this.Width());
canvas.BackgroundImage("bg.jpg");
canvas.PaintColor(COLOR_BLUE);
canvas.FontSize(10f);
imageSprite.Picture("bird.png");
imageSprite.X(this.Width() / 2);
imageSprite.Y((this.Height() / 2) - (imageSprite.Height() /
2));
imageSprite.Speed(12);
imageSprite.Initialize();
imageSprite.Interval(32);
imageSprite.Enabled(true);
ball.Radius(22);
ball.PaintColor(COLOR_BLUE);
ball.Speed(12);
ball.Initialize();
ball.Interval(32);
ball.Enabled(true);
}
class GameImageSprite extends ImageSprite
{
@Override
public void EdgeReached(int edge) {
// TODO Auto-generated method stub
this.Bounce((Integer) edge);
this.Heading(this.Heading() - 22);
}
public GameImageSprite(ComponentContainer container) {
super(container);
// TODO Auto-generated constructor stub
}
@Override
public void CollidedWith(Sprite other) {
// TODO Auto-generated method stub
super.CollidedWith(other);
this.Heading(this.Heading() - 55);
}
}
class GameBall extends Ball
{
@Override
public void EdgeReached(int edge) {
// TODO Auto-generated method stub
this.Bounce((Integer) edge);
this.Heading(this.Heading() - 22);
}
public GameBall(ComponentContainer container) {
super(container);
// TODO Auto-generated constructor stub
}
@Override
public void CollidedWith(Sprite other) {
// TODO Auto-generated method stub
super.CollidedWith(other);
this.Heading(this.Heading() - 22);