Possible misuse of evt.target.name. "Error #1010: A term is undefined and has no properties." - actionscript-3

So far this program is supposed to make an child object visible when the parent object is moused over, but I can't seem to get it to work. I'm in a little over my head here. Here's my code:
var iconCompArray:Array = new Array();
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMouse);
addIcons();
function addIcons():void
{
var iconComp:IconComp = new IconComp();
iconComp.x = stage.stageWidth/4;
iconComp.y = stage.stageHeight/4;
iconComp.iconImage.gotoAndStop(2);
iconComp.iconHighlight.visible = false;
iconComp.iconTitle.text = "Program X";
iconCompArray.push(iconComp);
addChild(iconComp);
}
function moveMouse(evt:MouseEvent):void
{
for (var i:int = 0; i < iconCompArray.length; i++)
{
if (evt.target.name == iconCompArray[i])
{
iconCompArray[i].iconHighlight.visible = true;
}
}
}
A new "iconComp" is pulled from the library and added to the stage through the iconCompArray. The idea was that when you mouse over the icon, a blue box would appear around it (iconHighlight.visible). But for some reason, what I have here doesn't work. I think I may have used evt.target.name incorrectly but I can't find a solution. Here's the error message that appears in the output:
TypeError: Error #1010: A term is undefined and has no properties.
at as3_fla::MainTimeline/addIcons()
at as3_fla::MainTimeline/frame1()

From the error you get the problem seems to be in the addIcons functions. You should check that the name of all your movie clip's childs is correct. The movie clip's property mouseChildren should be set to false. Also, it would be probably better if you use MOUSE_OVER and MOUSE_OUT events instead of MOUSE_MOVE.
function addIcons():void
{
var iconComp:IconComp = new IconComp();
iconComp.x = stage.stageWidth / 4;
iconComp.y = stage.stageHeight / 4;
iconComp.iconImage.gotoAndStop(2);
iconComp.iconHighlight.visible = false;
iconComp.iconTitle.text = "Program X";
iconComp.mouseChildren = false;
iconComp.addEventListener(MouseEvent.MOUSE_OVER, onIconOver);
iconComp.addEventListener(MouseEvent.MOUSE_OUT, onIconOut);
iconCompArray.push(iconComp);
addChild(iconComp);
}
And the event listeners should look like this:
function onIconOver(evt:MouseEvent):void {
IconComp(evt.target).iconHighlight.visible = true;
}
function onIconOut(evt:MouseEvent):void {
IconComp(evt.target).iconHighlight.visible = false;
}
You can also see the line of code that gives you that error if youy publish the swf in debug mode (Ctrl+Shift+Enter).

Related

Objects on the stage are not read when jumping to their frame

I am working on an Air desktop application. At some point when the user presses a button, it will jump to a specific frame, the problem is after going to that frame some Movieclips on the stage at that frame are not read though they were read from the very beginning.
The following error occurs.
Error #1009: Cannot access a property or method of a null object
reference.
I do not know why he can not read what is already on the stage, I guess it is something related to the arrangement of the layers. I noticed that sometimes though I do not find it logical, It is supposed to read ALL that is in the same frame as long as it is stopped on it, right?
The project is as follows:
1) At the very beginning an intro is played and then it goes to the first frame of the course.
2) At first frame the user chooses one of 5 buttons to click, based on each one it goes to different frame.
3) When the user is at any frame it should return to the main frame if he clicked a back button, this button command is gotoAndStop(1) and some conditional removeChild() to clean the stage from any objects generated by code depending on the frame the function was called from.
4) The problem arises when this back button is clicked, one or more of the very first 5 buttons suddenly disappears and an error generated as - for some unknown reason - it can not be read by the program any more, it can not read any events for it and generates the above error.
My code is as follows:
var myLettersLoader:URLLoader= new URLLoader();
mainMenu.addEventListener(MouseEvent.CLICK,gotomainMenu);
letters.addEventListener(MouseEvent.CLICK,showLetters);
lessons.addEventListener(MouseEvent.CLICK,showLessons);
revision.addEventListener(MouseEvent.CLICK,showRevision);
myLettersLoader.load(new URLRequest("data/letters/letters.xml"));
myLettersLoader.addEventListener(Event.COMPLETE,loadXML);
function showLetters(e:MouseEvent)
{
//gotoAndStop(2)
//aaaaa.alpha=1;
//aaaaa.visible=true;
Tweener.addTween(e.currentTarget, {width:originalWidth,height:originalHeight, time:0.25, transition:"linear"});
myPlace.visible = true;
myPlace2.visible = false;
myPlace3.visible = false;
jewels.visible = false;
mainContainer.visible=false;
close.visible=false;
studentBook.visible = false;
mainButton = e.currentTarget.name;
Tweener.addTween(myPlace, {alpha:1, transition:"linear"});
lettersContainer.visible=true;
Tweener.addTween(letterContainerText, {alpha:1, transition:"linear"});
for (var i=1; i<29; i++)
{
var letter = "L" + i;
myPlace[letter].id = i;
myPlace[letter].alpha = 1;
myPlace[letter].addEventListener(MouseEvent.CLICK,gotoLetterFrame);
myPlace[letter].buttonMode = true;
}
}
function showLessons(e:MouseEvent)
{
Tweener.addTween(e.currentTarget, {width:originalWidth,height:originalHeight, time:0.25, transition:"linear"});
myPlace.visible= false;
myPlace2.visible = true;
myPlace3.visible = false;
jewels.visible = false;
mainContainer.visible=false;
close.visible=false;
studentBook.visible = false;
lettersContainer.visible=true;
mainButton = e.currentTarget.name;
Tweener.addTween(myPlace2, {alpha:1, transition:"linear"});
studentBook.alpha = 0;
for (var i=0; i<5; i++)
{
var lesson = "Lesson" + i;
myPlace2[lesson].id = i;
myPlace2[lesson].alpha = 1;
myPlace2[lesson].addEventListener(MouseEvent.CLICK,gotolessonFrame);
myPlace2[lesson].buttonMode = true;
}
}
//=======================Revision functions==================================
function showRevision(e:MouseEvent)
{
Tweener.addTween(e.currentTarget, {width:originalWidth,height:originalHeight, time:0.25, transition:"linear"});
myPlace.visible= false;
myPlace2.visible = false;
myPlace3.visible = true;
jewels.visible = false;
mainContainer.visible=false;
close.visible=false;
studentBook.visible = false;
lettersContainer.visible=true;
mainButton = e.currentTarget.name;
Tweener.addTween(myPlace3, {alpha:1, transition:"linear"});
studentBook.alpha = 0;
for (var i=0; i<7; i++)
{
var revision = "Revision" + i;
myPlace3[revision].id = i;
myPlace3[revision].alpha = 1;
myPlace3[revision].addEventListener(MouseEvent.CLICK,gotoRevisionFrame);
myPlace3[revision].buttonMode = true;
}
}
//========================================================
function gotoLetterFrame(e:MouseEvent)
{
reloadButton.visible=true;
mainMenu.visible=true;
myClose.visible=true;
reloadButton.visible=true;
myNext.visible=true;
currentTarget=(e.currentTarget.id-1);
currentName = arrOfLetters[currentTarget];
xmlListOfClass=new XMLList(myxml.letter.(#id==currentName).children());
gotoAndStop(xmlListOfClass[counter].localName());
abc.visible=abcd.visible=true;
mainMenu.buttonMode=true;
}
function gotolessonFrame(e:MouseEvent)
{
reloadButton.visible=true;
mainMenu.visible=true;
myClose.visible=true;
reloadButton.visible=true;
myNext.visible=true;
currentTarget=(e.currentTarget.id);
xmlListOfClass = new XMLList(lessonsArr[currentTarget].lesson.children());
gotoAndStop(xmlListOfClass[counter].localName());
abc.visible=abcd.visible=true;
mainMenu.buttonMode=true;
}
function gotoRevisionFrame(e:MouseEvent)
{
reloadButton.visible=true;
mainMenu.visible=true;
myClose.visible=true;
reloadButton.visible=true;
myNext.visible=true;
currentTarget=(e.currentTarget.id);
myRevisionLoader.load(new URLRequest("data/revisions/"+currentTarget+"/revision.xml"));
myRevisionLoader.addEventListener(Event.COMPLETE,loadRevisionXML);
}
//=====================================
function loadLessonXML(e:Event)
{
lessonsArr[xx] = new XML(e.target.data);
xx++;
}
//==============================For revision==================================
function loadRevisionXML(e:Event)
{
revisionArr = new XML(e.target.data);
xmlListOfClass = new XMLList(revisionArr.revision.children());
gotoAndStop(xmlListOfClass[counter].localName());
abc.visible=abcd.visible=true;
mainMenu.buttonMode=true;
}
function loadXML(e:Event)
{
myxml = new XML(e.target.data);
}
//====================================
function gotomainMenu(e:MouseEvent)
{
gotoAndPlay(1);
}
This code is in the first frame, and in the second frame the button mainButton
is the button responsible for going back to frame 1
The buttons lessons,letters,revision disappears when returning to frame 1, or one of them sometimes with no logical reason
I solved it.It was all about the arrangement of layers.
All what I did is that I put the layer which contains the 5 buttons under the other layers. This surprisingly solved the problem. I do not know the reason till this moment but all what I know is that for some reason the caller of some event must be under the other layers containing some other objects or at least at the same layer.

AS3 Error drag and drop multiple draggable object to multiple target

I want to make the game drag and drop to create multiple draggable objects and it can dragged to multiple targets. But I encountered an error when I drag to the first object that I choose to certain target object is not dragged to the target and then I drag it to another target then succeed. I wanted to fix it. And if you can fix it when the object is dragged to the target and then want to be replaced by another object then the dragged object will be replaced and returned to its original position.
This is a overview the program
And this is the source codes that I use
var xPos:int;
var yPos:int;
var poin:int = 0;
var namaobj1:String;
var namaobj2:String;
addListeners(membaca, menulis, berenang, sepakbola, melukis, memasak, menari, bercocoktanam, beladiri, bermainmusik);
proses.addEventListener(MouseEvent.CLICK,proses1);
function getPosition(target:Object):void
{
xPos = target.x;
yPos = target.y;
}
function dragObject(e:MouseEvent):void
{
getPosition(e.target);
e.target.startDrag(true);
}
function stopDragObject(e:MouseEvent):void
{
if (e.target.hitTestObject(getChildByName("target2")))
{
e.target.x = getChildByName("target2").x;
e.target.y = getChildByName("target2").y;
namaobj2 = e.target.name;
}
else if (e.target.hitTestObject(getChildByName("target1")))
{
e.target.x = getChildByName("target1").x;
e.target.y = getChildByName("target1").y;
namaobj1 = e.target.name;
}
else
{
e.target.x = xPos;
e.target.y = yPos;
}
pil1.text = namaobj1;
pil2.text = namaobj2;
e.target.stopDrag();
}
function addListeners(... objects):void
{
for (var i:int = 0; i < objects.length; i++)
{
objects[i].addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
objects[i].addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
}
}
I'm using draggable objects by putting addListener function.
I also include his .fla can be downloaded here ( TEST.FLA ) rather you can more easily apply fixed codes
You can add Mouse_UP event in your targets instead of you dragged objects. Then when the MOUSE_UP event is trigged, you will know which objects is your target(e.target) without hitTestObject. The code is as follows:
You could have to set dragged object's mouseEnabled when mouse down, and true after dragged.

cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip?

So I'm creating a platform game in Actionscript 3.0, trying to call a function that spawns blocks based on an array. The code is in a 'Game' class and is directed towards a movieclip on my .fla
When it is ran I get the error:
"cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip."
Here's the code:
public function GameScreen(stageRef:Stage = null )
{
this.stageRef = stageRef;
btnReturn.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu, false, 0, true);
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
this.addEventListener(Event.ENTER_FRAME, createLvl);
this.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
this.stageRef.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
this.stageRef.addChild(blockHolder);
}
And
private function createLvl(event:Event):void
{
var lvlArray:Array = MovieClip(root)['lvlArray' +lvlCurrent];
var lvlColumns:int = Math.ceil(lvlArray.length/16);
for(var i:int = 0;i<lvlArray.length;i++){
if(lvlArray[i] == 1){
if(i/lvlColumns == int(i/lvlColumns)){
row ++;
}
var newBlock:Block = new Block();
newBlock.graphics.beginFill(0xFFFFFF);
newBlock.graphics.drawRect(0,0,50,50);
newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
newBlock.y = (row - 1)*newBlock.height;
blockHolder.addChild(newBlock);
} else if (lvlArray[i] == 'MAIN'){
mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
mcMain.y = (row-1)*newBlock.height;
}
}
row = 0;
}
Please help =(
Thanks!
First of all:
Turn on "permit debugging" in your publish settings. This will give you line numbers with your errors, so you can determine the exact location of your error.
Post the entire stack trace. That error by itself is not a lot to go on.
Given the error and the code you've posted, the error must be caused by your use of MovieClip(root). The root property does not always point to the main timeline in Flash, it will point to the Stage if the display object is added directly to the stage. For example:
trace(stage.addChild(new Sprite()).root) // [object Stage]
Documentation on root: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#root

Going from one scene to next and gettingType1009 Null object reference errors

Okay so in my main menu for my game, I have a glow effect so that when you hover over the invisible button 'startdesu', the script will make the whole menu glow, fading in and out. This code (from the event listener onwards) has been repeated for each button on the menu, with appropriate changes to the button instance name and the function names. The only problem is, when I click 'startdesu' and it takes me to the next scene, I start getting repetitive errors, "TypeError: Error #1009: Cannot access a property or method of a null object reference.
at bjvb_fla::MainTimeline/increaseGlow()". I've tried removing the glow event listeners when the buttons are clicked and everything but no luck. Please help! ;0;
Here is the essential code for brevity's sake, but I can post the whole thing if it helps.
(also, i get the same Null error for something else when i go from the game back to the start menu.
import flash.filters.*;
import flash.events.Event;
stop();
wow.alpha=0.5;
var menuGlow:GlowFilter = new GlowFilter();
menuGlow.color = 0xffffff;
menuGlow.blurX = 20;
menuGlow.blurY = 20;
menuGlow.alpha = 0;
menu.filters = [menuGlow];
var glow:Boolean = false;
startdesu.addEventListener(MouseEvent.MOUSE_OVER, addGlow);
startdesu.addEventListener(MouseEvent.MOUSE_OUT, removeGlow);
startdesu.addEventListener(Event.ENTER_FRAME, increaseGlow);
function addGlow(e:MouseEvent):void
{
glow = true;
}
function removeGlow(e:MouseEvent):void
{
glow = false;
}
function increaseGlow(e:Event):void
{
if(glow == true)
{
menuGlow.alpha = menuGlow.alpha + 0.02;
}
else if(glow == false)
{
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
This is the line that is likely causing the error:
menu.filters = [menuGlow];
There is probably no object with the instance name 'menu' in your second scene. You could fix the error by just adding a check if the object exists, like so:
function increaseGlow(e:Event):void
{
//if there's no menu object, return
if(!menu) return;
if(glow == true) {
menuGlow.alpha = menuGlow.alpha + 0.02;
} else {
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
But the correct solution would be to remove the event listeners. Not sure why it didn't work for you, but you should be able to just add this in the click event handler before you switch your scene.
startdesu.removeEventListener(Event.ENTER_FRAME, increaseGlow);
What errors do you get when you try to remove the event listener?

AS3 Cannot access a property or method of null object reference, issue with gotoAndPlay

so I'm still fairly new to AS3 and I'm getting my head around the errors and stuff and I'm trying to figure out why this is not working how it should, the function still executes but it leaves an error on my console. Here is my code:
import flash.events.MouseEvent;
import flash.display.MovieClip;
stop();
var activeHitArray:Array = new Array(word_select_box);
var activeDropArray:Array = new Array(answer1_word, answer2_word, answer3_word);
var hitPositionsArray: Array = new Array();
for (var numi:int = 0; numi < activeDropArray.length; numi++) {
activeDropArray[numi].buttonMode = true;
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_UP, mouseup1);
hitPositionsArray.push({xPos:activeDropArray[numi].x, yPos:activeDropArray[numi].y});
}
function mousedown1(event:MouseEvent):void {
event.currentTarget.startDrag();
setChildIndex(MovieClip(event.currentTarget), numChildren - 1);
}
function mouseup1(event:MouseEvent):void {
var dropindex1:int = activeDropArray.indexOf(event.currentTarget);
var target:MovieClip = event.currentTarget as MovieClip;
target.stopDrag();
if(target.hitTestObject(activeDropArray[dropindex1])){
// target.x = activeHitArray[dropindex1].x;
//target.y = activeHitArray[dropindex1].y;
if(answer1_word.hitTestObject(word_select_box)){
gotoAndStop("6");
}
} else {
target.x = hitPositionsArray[dropindex1].xPos;
target.y = hitPositionsArray[dropindex1].yPos;
}
}
And the Error I'm getting through is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_flv_fla::MainTimeline/frame6()
at flash.display::MovieClip/gotoAndStop()
at game_flv_fla::MainTimeline/mouseup1()
The only thing I can think of is that it is something to do with the gotoAndPlay() because when I place a trace into it's place I get no errors.
I copied your code, compiled and lauched it in Flash IDE. It works! :)
There were no errors.
But I know where the isue may lay. The addEventListeners are still on, no matter if there still are the objects they were linked to. You need to clean all active things before going to next frame:
if(target.hitTestObject(activeDropArray[dropindex1])){
if(answer1_word.hitTestObject(word_select_box)){
for (var i:uint = 0; i < activeDropArray.length; i++) {
activeDropArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[i].removeEventListener(MouseEvent.MOUSE_UP, mouseup1);
}
gotoAndStop("6");
}
}