Reputation: 3
Getting this error on AS3. TypeError: Error #1006: value is not a function. I'm new and cant understand why this error is occurring. Any help is greatly appreciated.
var wallet = false;
var bag = false;
var lunch = false;
wallet.addEventListener(MouseEvent.CLICK, walletClick);
lunch.addEventListener(MouseEvent.CLICK, lunchClick);
bag.addEventListener(MouseEvent.CLICK, bagClick);
function walletClick(event:MouseEvent)
{
if (wallet == true)
{
trace("You Already found your Wallet,look Elsewhere");
TextBox.text ="You Already found your Wallet,look Elsewhere";
}
else
{
trace("You found your Wallet");
wallet = true;
TextBox.text = "You found your Wallet";
}
}
function lunchClick(event:MouseEvent)
{
if (lunch == true)
{
trace("You Already found your Lunch Box,look Elsewhere");
TextBox.text ="You Already found your Lunch Box,look Elsewhere";
}
else
{
trace("You found your Lunch Box");
lunch = true;
TextBox.text = "You found your Lunch Box";
}
}
function bagClick(event:MouseEvent)
{
if (bag == true)
{
trace("You Already found your bag,look Elsewhere");
TextBox.text ="You Already found your bag,look Elsewhere";
}
else
{
trace("You found your bag under the Table");
bag = true;
TextBox.text = "You found your bag under the Table";
}
}
Getting this error on AS3. TypeError: Error #1006: value is not a function. I'm new and cant understand why this error is occurring. Any help is greatly appreciated.
Upvotes: 0
Views: 67
Reputation: 17859
you have 3 booleans:
var wallet = false;
var bag = false;
var lunch = false;
and then you add event listeners to them. but since they're not instances of EventDispatcher you get this error
Why do you do this?
You should add click event listeners to the display objects. If you have these display objects (named wallet, bag and lunch), then you are shadowing them with booleans here.
In this case just rename your vars here (and all their occurences, excluding lines with addEventListener
. For example to walletClicked
, bagClicked
, lunchClicked
And also: always use type annotations, since you're in as3.
var wallet:Boolean = false;
then wallet.addEventListener(...)
will cause compile-time error, not run-time error. Which is much easier to correct.
update:
You should also consider rewriting this code in a more generalized way to exclude the code repetitions:
var walletFound:Boolean = false;
var lunchFound:Boolean = false;
var bagFound:Boolean = false;
wallet.addEventListener(MouseEvent.CLICK, onItemClick);
lunch.addEventListener(MouseEvent.CLICK, onItemClick);
bag.addEventListener(MouseEvent.CLICK, onItemClick);
function onItemClick(event:MouseEvent):void {
var found:Boolean;
var itemName:String;
switch (event.currentTarget) {
case wallet:
found = walletFound;
itemName = "wallet";
walletFound = true;
break;
case bag:
found = bagFound;
itemName = "bag";
bagFound = true;
break;
case lunch:
found = lunchFound;
itemName = "lunch box";
lunchFound = true;
break;
default: return;
}
var text:String;
if (found) {
text = "You Already found your " + itemName + ",look Elsewhere";
} else {
text = "You found your " + itemName;
}
trace(text);
TextBox.text = text;
}
not tested it, but should work.
Upvotes: 2