EventDispatcher.registerEventForDelegation confusion

51 views
Skip to first unread message

Kevin Moran

unread,
Feb 8, 2012, 10:13:15 PM2/8/12
to android-progr...@googlegroups.com
I'm a bit confused on how to use EventDispatcher.registerEventForDelegation.

How do I know the eventName and componentID? For example, if I want to handle touches anywhere to fire off something, how would I do it? The documentation just gives the method signature.

Kevin

Dave Wolber

unread,
Feb 8, 2012, 11:53:53 PM2/8/12
to Android Programming at USF
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);

Kevin Moran

unread,
Feb 8, 2012, 11:56:19 PM2/8/12
to android-progr...@googlegroups.com
Hi Dave,

So I was using the subclass style of doing it, however it wasn't picking up any of my touch events. I think it may have been an issue with android + eclipse. In the touched method I would simply print out what the x,y values were but they never showed up in the logcat. Also for some reason, before the canvas was even set up, x and y coordinates were getting printed out to the logcat. In other words, somehow the touched method was being called before the canvas was set up, but the touched method wouldn't be called when I was actually touching the screen.

Kevin

Dave Wolber

unread,
Feb 9, 2012, 12:00:39 AM2/9/12
to Android Programming at USF
Hmmm... I'll try an example with the Canvas, can you send me your
code?

Dave

Dave Wolber

unread,
Feb 9, 2012, 12:02:52 AM2/9/12
to Android Programming at USF
Also the MoleMash sample on course page is catching canvas touched
events, see https://sites.google.com/site/mobileprogrammingusf/mole-mash-java-bridge

Is it possible you also registered for it so that subclassed event
wasn't being called?

Kevin Moran

unread,
Feb 9, 2012, 12:06:19 AM2/9/12
to android-progr...@googlegroups.com
@Override
public void Touched(float x, float y, boolean touchedSprite)
{
Log.d("mine", "not printing");
}

All I want to do is print that. I think my logcat is broken. It's not showing anything even when the first thing the app does is print out "hello".

Kevin
Reply all
Reply to author
Forward
0 new messages