3 Replies - 6095 Views - Last Post: 22 November 2009 - 09:37 AM

#1 cdmoody  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 09

ActionScript 3 for True False Quiz

Posted 21 November 2009 - 09:44 PM

Hi, I'm fairly new to ActionScript. I'm trying to create a tutorial with a quiz at the end of it. My assignment instructions are: "Each button will have an event listener to execute a function when the button is clicked. The function will receive the event and will use the eventTarget property to determine if the correct button was indeed the one chosen by the user."

I got the True False on the first question to work then after that I couldn't get it to work. Here's the code that I currently have. I'm getting an error that I'm using a duplicate function definition. I assume this is because I've used the code in the previous frame. I'm just not sure how to make this modular so I can use it on all of my questions. Any help is appreciated!

true_btn.addEventListener(MouseEvent.CLICK, trueAns);

function trueAns(event:MouseEvent)
{
	feedback.text = "Correct answer!";
	event.target.alpha = 1;
}


false_btn.addEventListener(MouseEvent.CLICK, falseAns);

function falseAns(event:MouseEvent)
{
	feedback.text = "Sorry, that's wrong.";
	event.target.alpha = .5;
}



Is This A Good Question/Topic? 0
  • +

Replies To: ActionScript 3 for True False Quiz

#2 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: ActionScript 3 for True False Quiz

Posted 22 November 2009 - 04:18 AM

Very easy one.

When you define by function, you will get this error if you create a function with same name on one timeline. I think that is what you did - on each frame you redefined those.

Ok, so to make this work, use this code:
true_btn.addEventListener(MouseEvent.CLICK, trueAns);

root.trueAns = function(event:MouseEvent)
{
	feedback.text = "Correct answer!";
	event.target.alpha = 1;
};


false_btn.addEventListener(MouseEvent.CLICK, falseAns);

root.falseAns = function(event:MouseEvent)
{
	feedback.text = "Sorry, that's wrong.";
	event.target.alpha = .5;
};


In every frame you redefine the functions (even the first one for better organization).
Was This Post Helpful? 0
  • +
  • -

#3 cdmoody  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 09

Re: ActionScript 3 for True False Quiz

Posted 22 November 2009 - 08:52 AM

When I use that code I get these errors now.


1119: Access of possibly undefined property trueAns through a reference with static type flash.display:DisplayObject.
1120: Access of undefined property trueAns.
1120: Access of undefined property falseAns.
1119: Access of possibly undefined property falseAns through a reference with static type flash.display:DisplayObject.
Was This Post Helpful? 0
  • +
  • -

#4 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: ActionScript 3 for True False Quiz

Posted 22 November 2009 - 09:37 AM

Do you really need to redefine those functions? I mean... The functionality is still the same.
Therefore you can add this at the first frame:
function true_ans(e:MouseEvent){
...
}

function false_ans ...


Note: have this code only once in the file - ie. just one keyframe, others either blank or just without keyframe.

And, in a separate layer this:
false_btn.addEventListener(MouseEvent.CLICK, falseAns);
true_btn.addEventListener(MouseEvent.CLICK, trueAns);


Note: do this on every (key)frame of that layer.

This post has been edited by Aurel300: 22 November 2009 - 09:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1