1

I am trying to add the same button function to 2 different symbols in Flash. One is the logo and the other is text that I converted to a symbol that will show itself during the end scene.

I don't understand what I am doing wrong, but I am extremely new to Actionscript & Flash.

My code looks like this:

import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.net.navigateToURL;
import flash.net.URLRequest;

myButton, txtButton.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
function onClick(e:MouseEvent):void{
    navigateToURL(new URLRequest("http://www.true.land"), "_blank"); 
}

But I am getting this error:

Attempting to launch and connect to Player using URL C:\Users\Angela\Desktop\ASU\GIT 314\Assignment 7\AngelaRogers_Assignment7.swf [SWF] C:\Users\Angela\Desktop\ASU\GIT 314\Assignment 7\AngelaRogers_Assignment7.swf - 351066 bytes after decompression TypeError: Error #1009: Cannot access a property or method of a null object reference. at Button/frame1()[Button::frame1:7]

5
  • try with no coma...txtButton.addEventListener(MouseEvent.CLICK, onClick, false, 0, true); then myButton.addEventListener(MouseEvent.CLICK, onClick, false, 0, true); and you must have a myButton and txtButton elements available in the scene (make sure the movieclips are there and properly named) (otherwise you will have a null reference error (elements can't be found))) hth Commented Apr 30, 2014 at 20:04
  • @GeorgeProfenza That did not work. Now I am getting a duplicate definition error. My elements are available in the scene, that is not the issue. Commented Apr 30, 2014 at 20:21
  • 3
    If you're getting a duplicate definition error you're probably copying the onClick function(function onClick(e:MouseEvent)...) (which you should, don't need to). you need to add the listener to different movie clips Commented Apr 30, 2014 at 20:24
  • @GeorgeProfenza Sorry George, I really am very new to coding events so I don't understand what you are saying. I created a new line of code for the txtbutton and it still doesn't work. Commented Apr 30, 2014 at 21:08
  • well done for figuring out and good luck :) Commented Apr 30, 2014 at 21:11

2 Answers 2

1

You must write it twice - once for each button, starting with only it's name: child.addEventListener. There is no shortcut to add the same for two objects at once.

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

1 Comment

Thanks, I was trying to create a shortcut that didn't exist.
1

You can write a shortcut function to do this easily enough. (As pointed out by others, your comma is what is causing the error). This I believe is more what you're after: (especially if you keep adding more buttons).

import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.net.navigateToURL;
import flash.net.URLRequest;

addClick(myButton, txtButton); //you can add as many items as you want as parameters

function addClick(...buttons):void {
    //the ...buttons parameter is known as the '...rest' parameter, and is an array of all the parameters passed to the function
    for each(var btn:Sprite in buttons){ //loop through all the items passed in and add the listener
        btn.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
    }
}

function onClick(e:MouseEvent):void{
    navigateToURL(new URLRequest("http://www.true.land"), "_blank"); 
}

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.