Author Topic: Totally stucked ... can't set an "addEventListener"  (Read 1799 times)

Tipo

  • Member
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
I don't know how to set/get an Event from an object so I decided to try it with a listener.

Code: [Select]
override public function create():void
{
for (var i:uint = 0; i < 8; i++) {
for (var j:uint = 0; j < 8; j++) {
aVeggie = new Veggie(Math.random()*5+1);
aVeggie.x = i * 32;
aVeggie.y = j * 32;
add(aVeggie);
aVeggie.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown)
}
}
}

But when I try that last line to set a listener I get this failure:
Quote
D:\Flash\VeggieMatch\src\GameState.as(31): col: 14 Error: Call to a possibly undefined method addEventListener through a reference with static type Veggie.
Why is it undefined?
At the top I wrote
Code: [Select]
import flash.events.*;
I just want to have a Listener that watchs at each object if there is a "mouse event" on that object.

Is that the wrong way?

baconandgames

  • Global Moderator
  • Member
  • *****
  • Posts: 34
  • Karma: +0/-0
  • "Whatever you are, be a good one."
    • View Profile
    • Bacon and Games
Re: Totally stucked ... can't set an "addEventListener"
« Reply #1 on: Fri, Dec 14, 2012 »
You should take a look at Photonstorm's Flixel Power Tools. They add a lot of really handy stuff to Flixel, like click "events" (they're really callback functions).

Once you've downloaded the tools and included them in your project there's two steps.

  • Enable the plugin
  • Make sprite clickable and define your callback

In your PlayState
Code: [Select]
override public function create():void
{

// enable sprite click plugin
if (FlxG.getPlugin(FlxMouseControl) == null){
FlxG.addPlugin(new FlxMouseControl);
}

                 ....


Then on your sprite might look something like this
Code: [Select]
package
{
import org.flixel.plugin.photonstorm.FlxExtendedSprite;
import org.flixel.FlxG;

public class Trap extends FlxExtendedSprite
{
public function Trap(X:Number=0, Y:Number=0, SimpleGraphic:Class=null)
{
super(X, Y, SimpleGraphic);
                        this.clickable = true;
this.mouseReleasedCallback = handleClick;
}

protected function handleClick(obj:FlxExtendedSprite,x:int,y:int):void{
FlxG.log("clicked"); // handle click here
}

}
}

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Totally stucked ... can't set an "addEventListener"
« Reply #2 on: Fri, Dec 14, 2012 »
Tipo - just to explain why what you're doing doesn't work: Flixel objects are not traditional Flash DisplayObjects, which is why you can't add event listeners to them (or do any of the other stuff you normally do with MovieClips/Sprites).
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Totally stucked ... can't set an "addEventListener"
« Reply #3 on: Fri, Dec 14, 2012 »
The simplest way to achieve your goal is to make veggie extend FlxButton. Then you can replace the event listener line with:

Code: [Select]
veggie.onDown = mouseDown;

A button is like a sprite plus built-in mouse events, so you can do pretty much anything that you would do with a sprite with it.

Tipo

  • Member
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Totally stucked ... can't set an "addEventListener"
« Reply #4 on: Sat, Dec 15, 2012 »
You should take a look at Photonstorm's Flixel Power Tools. They add a lot of really handy stuff to Flixel, like click "events" (they're really callback functions).

I've just implemented the Flixel Power Tools but it seems that I have a lack of knowledge how to use it.

My "Sprite"-class named "Veggie" looks like this:
Code: [Select]
package 
{
import org.flixel.*;
import org.flixel.plugin.photonstorm.FlxExtendedSprite;

//public class Veggie extends FlxSprite
public class Veggie extends FlxExtendedSprite
//public class Veggie extends FlxButton
{
[Embed(source = '../assets/veggies.png')]
private const ImgVeggies:Class;
private var type:uint;

public function Veggie(_type:uint)
{
loadGraphic(ImgVeggies, true, false, 32, 32, false);
addAnimation("carrot", [0]);
addAnimation("eggplant", [4]);
addAnimation("pumpkin", [8]);
addAnimation("corn", [12]);
addAnimation("pepper", [16]);

type = _type;

switch (type)
{
case 0:
play("carrot");
break;
case 1:
play("eggplant");
break;
case 2:
play("pumpkin");
break;
case 3:
play("corn");
break;
case 4:
play("pepper");
break;
}
}

override public function update():void
{
super.update();
}

}

}

But when I tried to copy your "Trap"-class into this it won't work.
I got an error when I try to expand the parameters of my constructor with your Trap-class. Maybe it's wrong what I do.
I also tried to create an own class called "Trap" and then import it in my "GameState.as" but I can't get it to work.
I would be so happy if I could see "clicked" in my shell but there is nothing, so I think I implemented it the wrong way.  :'(

Maybe these is a bit more for understanding my dilemma instead of posting pieces of code:
https://github.com/TKlement/VeggieMatch.git

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Totally stucked ... can't set an "addEventListener"
« Reply #5 on: Sat, Dec 15, 2012 »
You need to call super(); at the top of your Veggie class constructor.

Tipo

  • Member
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Totally stucked ... can't set an "addEventListener"
« Reply #6 on: Sat, Dec 15, 2012 »
Like this?

Code: [Select]
// constructor
public function Veggie(_type:uint, X:Number=0, Y:Number=0, SimpleGraphic:Class=null)
{
super(X, Y, SimpleGraphic);
        this.clickable = true;
this.mouseReleasedCallback = handleClick;

loadGraphic(ImgVeggies, true, false, 32, 32, false);
addAnimation("carrot", [0]);
addAnimation("eggplant", [4]);
addAnimation("pumpkin", [8]);
addAnimation("corn", [12]);
addAnimation("pepper", [16]);

type = _type;

        switch (type)
        ...
}

That's interesting!
When I put this 3 lines at the bottom of my constructor I get an error that I can't access a property or method of an null-object reference.

But this solution has no error but also no "clicked" in the shell.  :'(

I don't understand that.
What does this function do?
Does it create a event-handler for every object that I create in my for-loop?
And what does this "SimpleGraphic" do? I can't find it in my libraries.

Sorry for my questions. :(

Edit:
Ok, now I added some parameters but I'm not really sure if this is right.
In my GameState.as where I create my objects in a for-loop:
Quote
// GameState.as
override public function create():void
{
   // enable sprite click plugin
   if (FlxG.getPlugin(FlxMouseControl) == null){
      FlxG.addPlugin(new FlxMouseControl);
   }
   
   for (var i:uint = 0; i < 8; i++) {
      for (var j:uint = 0; j < 8; j++) {
         aVeggie = new Veggie(Math.random()*5+1, i * 32, j * 32);
         aVeggie.x = i * 32;
         aVeggie.y = j * 32;
         add(aVeggie);
      }
   }
}

I added the X and Y params to the Veggie-object but for now it seems it is not enough.
When I trace the call in the Veggie.as I get a "null" from the SimpleGraphic.

Edit 2:
OMG it works!!!
Yipiiehh!
Many thanks!

Sorry, I don't know why but when I use "trace" instead
FlxG.log("clicked");
I can see "clicked" in the shell.

Ok from now on I can touch every object.
This is so great!  :) :) :)

I hope I find somewhere another tutorial for the drag and drop functions.
« Last Edit: Sat, Dec 15, 2012 by Tipo »

baconandgames

  • Global Moderator
  • Member
  • *****
  • Posts: 34
  • Karma: +0/-0
  • "Whatever you are, be a good one."
    • View Profile
    • Bacon and Games
Re: Totally stucked ... can't set an "addEventListener"
« Reply #7 on: Sat, Dec 15, 2012 »
At the bottom of the page, Photonstorm has included some sample code for dragging sprites. You can also pick through the demo code provided with Flx PT package that you downloaded

http://www.photonstorm.com/flixel-power-tools/flxextendedsprite