Clickable addGraphic within a class?


(Nate ) #1

Hey guys!

I have a simple dialog box class that when I create it accepts different arguments to populate different options on the screen. In the dialog class this is done by simply adding the passed strings. What I am trying to do now, is via the dialog class, make the different strings clickable.

Relevant code from the Dialog class:

this.addGraphic(choice);

Relevant code from the Dialog Update method:

override public function update():void
        {
            if (collidePoint(choice.x, choice.y, world.mouseX, world.mouseY))
            {
                if (Input.mouseReleased) click();
            }
        }
        
        public function click():void
        {
            trace("clicking added graphic");
        }

I feel like I am doing something fundamentally incorrect…

Thanks guys!


Can Graphiclist get the property of a button?
(Ultima2876) #2

Make sure the entity’s hitbox is set correctly; collidePoint uses the entity’s hitbox to determine collision (as do all collision functions in Entity). I believe that may be the problem :slight_smile:


(Nate ) #3

You are right, it did need a hitbox, however how would I add a hitbox to just an added graphic that is text?


(Zachary Lewis) #4

addGraphic() returns an Entity. If you want to add text and set a hitbox without making a custom Entity, you can do something like this:

var clickMe:Entity = addGraphic(new Text("Hello world!"));
clickMe.setHitbox(20, 8);

(Nate ) #5

Hey Zach! Thanks for the reply! I am not sure why I didn’t think to do this. However I am getting an unrelated type error doing this. Also worth noting, I am not adding a new Text my code looks like this:

    var clickMe:Entity = this.addGraphic(titlehandler);
            clickMe.setHitbox(titlehandler.width, titlehandler.height);

(Zachary Lewis) #6

What is the error? It will usually give you a line number.


(Nate ) #7

It is underlining add in addGraphic and saying Implicit coercion of a value of type net.flashpunk:Graphic to an unrelated type net.flashpunk:Entity.


(David Williams) #8

Where do you declare “titlehandler”?


(Ultima2876) #9

Is titlehandler a Graphic type or an Entity type?

Still seems odd because the error says you can’t convert a Graphic to an Entity… I’d have thought if it was a problem with titlehandler being an Entity it would’ve given the error the other way around (“cannot convert Entity to Graphic”).

I checked the docs and addGraphic definitely does return an Entity type so the return value isn’t the problem either.


(Jacob Albano) #10

Are you doing addGraphic() on the Entity, or the world?

This returns an entity:

world.addGraphic(g);

And this returns a graphic:

entity.addGraphic(g);

(Ultima2876) #11

Oh! He said from the ‘Dialog’ class, so I’m guessing it’s in an Entity and the latter. Good spot.


(Nate ) #12

This is all happening in the Dialog class like Ultima said. My Dialog class looks like this:

            titlehandler.text = "Derp";
            titlehandler.size = 24;
            titlehandler.x = 500 / 2 - titlehandler.width / 2;
            titlehandler.y = 50

            //clickable text
            var clickMe:Entity = this.addGraphic(titlehandler);
            clickMe.setHitbox(titlehandler.width, titlehandler.height);

            this.addGraphic(titlehandler);

(Jacob Albano) #13

Try:

var clickMe:Entity = world.addGraphic(titlehandler);
clickMe.setHitbox(titlehandler.width, titlehandler.height);

(Nate ) #14

It is still yelling at me telling me Implicit coercion of a value of type String to an unrelated type net.flashpunk:Graphic.


(Jacob Albano) #15

String? wut

What type is titleHandler?


(Nate ) #16

titleHandler is text lol

I am trying to get clickable text that is added inside of an entity class with it’s own hitbox, if that makes sense…

EDIT: Actually I think it is a String…

Here is what I am doing, I have a messageBox class which is extending Entity, inside of this class I create a box image and I have two private text variables. When I place a messageBox into the gameworld, I have it so the messageBox requires two Strings. A title string, and a message string. Then I set the text value of the two previously mentioned private class text variables to title and message strings. Which all works fine and they display on the screen no problem, and it makes for a really fast messaging system.

What I am trying to do, is add a third parameter like option, then when I add a messageBox entity it will require, title, message, option. And I would like the option to be clickable.

Let me know if you need more of my code! :smiley:


(billy2000) #17

I may be wrong,but… I think it gives you this error because your variable its a string. Try to put the string into a text variable and then try to add it as a graphic.