0

I have a class named TextLink. The text is meant to be clicked on and it should dispatch an event (which I'm not too concerned about yet)... currently it just prints out a message. The class takes an x, y, and a string to set the text. Dead simple... But it crashes the browser.

Instance calls in main:

package {
    import flash.display.Sprite;

    import nav.text.TextLink;

    public class test_array_of_objects extends Sprite
    {
        public function test_array_of_objects()
        {
            var ary:Array = new Array(5);
            var i:uint;
            var ty:uint;
            var tx:uint = 30;

            for(i=0; i<ary.length; i++)
            {
                ty = i * 20 + 20;   
                var tmp:TextLink = new TextLink(tx, ty, "some text" + i.toString());
                ary.push(tmp);
            }           
        }
    }
}

Class:

package nav.text
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.external.ExternalInterface;

    public class TextLink extends Sprite
    {
        public var tf:TextField = new TextField();

        public function TextLink(tx:uint, ty:uint, tft:String)
        {   
            tf.text = tft;
            tf.x = tx;
            tf.y = ty;
            tf.autoSize = TextFieldAutoSize.LEFT;

            addChild(tf);
        }

        private function rollfunc(e:Event):void
        {
            ExternalInterface.call("console.log", "got a clicky");  
        }

        /*
        protected function rollfunc(e:Event):void
        {   //dispatch a custom event 
            dispatchEvent(new Event(Event.COMPLETE));   
        }
        */
    }
}

You'll notice that I have commented out the rollfunc function because I was going to add it later- What I would like to do here is to dispatch an event for whoever is listening to the class so that I can do something specific with the event of clicking on the text. The instance would be defined by an addEventListener() call.

Thanks

7
  • you keep changing the content of your question without any explanation. The original question had syntax that was completely wrong, and the corrections below that you have added should allow you to compile and run something. You don't want to "talk to" the text links, you want them to broadcast information. Commented Dec 19, 2009 at 8:24
  • i've changed the above content to show you the details of what is crashing the browser. thanks for the help. Commented Dec 19, 2009 at 8:27
  • FWIW, I was told earlier today that the etiquette on this site is to alter the original and re-refer to it rather than posting an answer to my own question. I hope this is correct information. Commented Dec 19, 2009 at 8:30
  • your solution doesn't really answer my question. what i meant to say was that i would like for the elements in the array (as OBJECTS) to be individually addressable so that i can control them by name. a simple example of this would be ary[i].x = 50; does this make more sense? Commented Dec 19, 2009 at 8:43
  • @jml, RE: SO usage--indeed, only answers should be posted as answers. Clarifications are made by editing the question. However, at a certain point you wind up with a new question (at which point this happens isn't completely well-defined). If you edit your question such that an answer no longer applies, consider either adding a note about the old content (though the "edited" link can reveal this) or posting a new question. I don't currently have advice as to when either option is appropriate. Commented Dec 19, 2009 at 9:30

4 Answers 4

3
for(i=0; i<ary.length; i++)
{
    ...
    ary.push(tmp);
}

This is an infinite loop. ary.push() will increase ary.length on each iteration and i will never be able to catch up to it.

I think you want @outis's second suggestion here; i.e. ary[i] = tmp

Or just create an empty array and push things into it.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah; i think you're right about this. thank you for your observation... that helps a lot.
1

ary[i] is an undefined element of the array ary, so ary[i].push will throw an exception. You probably want either:

ary.push(tmp);

or

ary[i] = tmp;

but I can't say for certain, since you didn't say what behavior you're getting, you merely stated that the code "won't work".

1 Comment

the browser crashes, whether i am debugging or not. i have a listener being added in the textlink class, but this shouldn't matter too much.
1

You got several problems going on here that might be causing your snippet to fail. The use of ary[i].push(tmp) is improper. Doing this would assume that ary[i] is itself another array that you would be pushing (appending to). I personally wouldn't use uint in this fashion either, just as a general practice. It is actually slower than int and serves no solid purpose here. Additionally, instead of creating an array of a specific length, I will use constants as shown below. Arrays are mutable, so the length isn't relevant, but this is just a stylistic concern.

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Test extends Sprite
    {
        private static const NUM_SPRITES:int = 15;

        private var ary:Array;

        public function Test()
        {
            var i:int;
            var ty:int;
            var tx:int = 30;

            ary = [];

            for(i=0; i<NUM_SPRITES; i++)
            {
                ty = i * 20 + 20;
                var tmp:Sprite= new Sprite();
                tmp.addEventListener(MouseEvent.CLICK, handleClick);
                tmp.graphics.beginFill(0xFF0000);
                tmp.graphics.drawRect(0,0,20,20);
                tmp.x = tx;
                tmp.y = ty;
                addChild(tmp);
                ary.push(tmp);
            }
        }

        public function handleClick(event:MouseEvent):void
        {
            for each(var spr:Sprite in ary)
            {
                if(spr == event.target)
                    trace(spr.x, spr.y);
            }
        }
    }
}

3 Comments

can you explain to me how or why uint would be slower than int? this simply shouldn't be, by definition. i cam imagine though, that uint is emulated rather than actually smaller in runtime terms.
i also don't understand why you would want to put a for loop inside of your handleClick function in order to determine the target of the event. wouldn't it be easier to just call a handleClick function from the listener you'd already made in the previous for loop?
Hi again Joel, I have a post here that I cannot figure out: stackoverflow.com/questions/1992456/… do you have any idea of what I could do here to remove the items from the stage? I don't think I need a utility wrapper for such simple functionality, esp. because I am not going to make a new one for each different type of loading-oriented sprite-class.
0

I see you 'newing' up a TextLink object but doing nothing with it.

Did you mean to add that to the array 'ary' ?

As it stands, you're creating this new instance and it goes out of scope as the loop iterates.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.