typeError: Error #1006: setChildIndex is not a function - actionscript-3

Do you know what causes this error?
TypeError: Error #1006: setChildIndex is not a function.
at Function/()[iVision_Game_fla.MainTimeline::frame83:151]
Here's where I think the error occurs...
function dragObject(indx1:int,indx2:int):Function {
return function(event:MouseEvent){
var item:MovieClip=MovieClip(event.currentTarget);
item.startDrag();
var topPos:uint=this.numChildren-1;
var itemSound:Sound = new Sound();
itemSound.load(new URLRequest("sounds/"+dirArray[indx1])+indx2+".mp3"));
if(!activeSound)
{
itemSound.play();
activeSound=true;
}
this.setChildIndex(item, topPos);
}
}
//calling it on another function
var functionOnDrag:Function = dragObject(indexc[count-1],index[count2]);
pb[i].addEventListener(MouseEvent.MOUSE_DOWN,functionOnDrag);

I think scope is the problem here. If the item's parent isn't changing, you can use item.parent.setChildIndex(item, topPos)
Also, before that, topPos:uint = item.parent.numChildren -1;
Update:
To debug further, put a breakpoint on the line of setChildIndex (151). (adding a breakpoint: click to the left of the line number. it will add a little red circle. When code hits this in debug mode it will stop.)
Then go to Debug -> Debug Movie -> Debug
When it stops, open the Debug Panels: Window->Debug Panels->Variables, Window->Debug Panels->Callstack
In the variable panel, you should see all the current variables in the scope. What you are looking for is an issue with the object that setChildIndex is being called on. In this case, the parent of item. You can drill down to see what that is. It should be a DisplayObjectContainer (MovieClip, Sprite, etc...). Item came from event, so i would check event.currentTarget as well. You are basically trying to confirm that at that breakpoint, item exists, it has a parent, and it's parent is a DisplayObjectController. If one of these isn't true, you can track backwards from here why this isn't the case.

TypeError: Error #1006: setChildIndex is not a function
You have created anonymous function, if you want capture this, you could do such construction:
var selfRef:MyClass = this;
Now your handler for the MouseEvent will look:
function dragObject(indx1:int, indx2:int):Function {
return function (event:MouseEvent) {
//Your code
selfRef.setChildIndex(item, topPos);
}
}

Related

TypeError: Error #1010: A term is undefined and has no properties?

Error:
TypeError: Error #1010: A term is undefined and has no properties. at Untitled_2_fla::MainTimeline/frame1()[Untitled_2_fla.MainTimeline::frame1:4]
Can't seem to figure out what's that about.
Here's my code:
import flash.events.MouseEvent;
btnMc1.txtSourceMc.gotoAndStop();
btnMc1.addEventListener(MouseEvent.ROLL_OVER, over);
btnMc1.addEventListener(MouseEvent.ROLL_OUT, out);
function over(e: MouseEvent) {
btnMc1.buttonMode = true;
btnMc1.gotoAndPlay(1);
}
function out(e: MouseEvent) {
btnMc1.gotoAndPlay(62);
}
You need to create the variable btnMc1, such as var btnMc1 And you're going to have to give it a class such as var btnMc1:
whatEverObjectICreated = new whatEverObjectICreated;
btnMc1.txtSourceMc.gotoAndStop();
Change that to
btnMc1.txtSourceMc.gotoAndStop(1);
Make sure that you've named you MCs correctly. Firstly, go to your stage, and click on btnMc1. Make sure you've name it as such in the properties panel(windows -> properties). Next, double click onto btnMc1 and do the same for txtSourceMc, make sure that it has the correct name. Im assuming that you have txtSourceMc inside btnMc1 on the timeline.
If the problem persists, it is probably because whatever you're loading into btnMc1 or txtSourceMc is too huge. So it cannot instantiate on the frame you're trying to call it in this code, thus it returns as undefined.
To solve this, right click onto btnMc1 from the library and select properties. Then select export for actionscript and make sure that the box for exporting on frame1 is checked. Do the same for txtSourceMc.

Flash AS3 ReferenceError: Error #1056: Cannot create property

I am following a tutorial on making a rhythm game in Flash, and am new to AS3. I keep getting this error:
ReferenceError: Error #1056: Cannot create property destroy on flash.display.Shape.
at source_fla::MainTimeline/removeButtons()[source_fla.MainTimeline::frame5:27]
at btnSongSelect/clickThis()[btnSongSelect::frame1:25]
referring to the following code:
//this function will remove all of the buttons from the stage
function removeButtons():void{
//we're going to use the same loop
for(var i:int=0;i<numChildren;i++){
var remove = getChildAt(i);
//set the target's destroy variable to true
remove.destroy = true;
}
}
Can someone please explain to me the cause of the error and a possible fix? Thank you.
It happens because getChildAt(i) returns a DisplayObject instance that doesn't have a destroy property.
A possible solution (based on the comment
//this function will remove all of the buttons from the stage
) is to check if(remove is ButtonClass) which is expected.
Another option is as Yasuyuki Uno suggests here is checking if(remove.hasOwnProperty('destroy'))

Actionscript how to get name of instance using getChildByName

Okay so I have a MovieClip called sC and need to write a code where, if you click the button (sC) then sC will dissapear. The function needs to work for multiple buttons. What I tried was
sC.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
var self;
self = MovieClip(getChildByName(event.target.name));
self.visible=false;
Now when I try this code, it gives me an error when I click sC. It says "cannot access a property or method of a null object reference.". when I try to trace(self) it outputs "null". Is there a way where I can get the name of the instance of the object which is using the clicKHandler function and then make it's visibilty equal to false (visible=false)?
Note that when I trace(event.target.name) it says "instance127".
In your code, the variable self resolves to your movieClip's name, but not the complete path to where it exists. Try setting it up like below, where target is the button that was clicked:
sC.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void
{
event.target.visible = false;
}

as3 - addchild with drag and drop

I am trying to add a child instance of an object to the stage, then allow the user to drag and drop this object (in this case, a movie clip) on the stage. However, I am getting the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at working_copy_fla::MainTimeline/dragObject()
So, that is my first problem. Then second problem, is I have not found an answer as to how to make a child object (specifically, a movie clip) able to properly be dragged and dropped on the stage.
Here is my code:
// Allow buttons to bring objects to the stage
myButton.addEventListener(MouseEvent.CLICK, addImage);
function addImage(event:MouseEvent):void
{
var myImage:Image_mc = new Image_mc();
stage.addChild(myImage);
// Center the object
myImage.x = 300;
myImage.y = 300;
// Allow the object to be drag and dropped
myImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
myImage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
function startDragging(event:MouseEvent):void
{
event.target.x = event.target.parent.mouseX - event.target.mouseX
event.target.y = event.target.parent.mouseY - event.target.mouseY
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
function dragObject(event:MouseEvent):void
{
event.target.x = event.target.parent.mouseX - event.target.mouseX
event.target.y = event.target.parent.mouseY - event.target.mouseY
}
function stopDragging(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
EDIT
I figured it out, and the solution was as simple as looking to the sample code in Adobe Flash (using CS6). Here is my code now:
// Allow buttons to bring objects to the stage
myButton.addEventListener(MouseEvent.CLICK, addImage);
function addImage(event:MouseEvent):void
{
var myImage:Image_mc = new Image_mc();
stage.addChild(myImage);
// Center the object
myImage.x = 300;
myImage.y = 300;
// Allow the object to be dragged
myImage.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
}
function clickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function releaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}
The key here was that I have created universal functions (clickToDrag and releaseToDrop) that will accept input from any object (so I can re-use these functions with other images that I add to the stage). This code works with multiple children on the stage (all can be drag and dropped at any time).
The only problem I am having with it now is that I am getting this error whenever I spawn a child element (by clicking on the myButton button instance):
ReferenceError: Error #1069: Property stopDrag not found on flash.display.SimpleButton and there is no default value.
at working_copy_fla::MainTimeline/releaseToDrop()
This error is not stopping the application from working; everything still runs fine. But I would still like to figure out why this error is occuring. My guess is that whatever is using "stopDrag" (should just be a movie clip) is not capable of that method.
event.target.x = event.target.parent.mouseX - event.target.mouseX
The above code assumes that 'event.target' is the stage, right (because you added the listener to the stage)? So you're trying to change the x/y of the stage? No. The start/stopDragging should make a reference to the object being dragged, available as a private class variable, which is visible to the dragObject method. Also, what is the stage's parent ('event.target.parent.mouseX')? There is no parent to the stage. This is probably what the "null object reference" is refering to.
EDIT
I'm used to Object-Oriented AS3 (highly recommended), but I'm guessing that programming on the 'timeline' the following should work. Declare a variable outside of your functions, something like:
var objectCurrentDragging:DisplayObjectContainer;
Then in your 'addImage' function, use the following code to make objectCurrentDragging reference the object which you want to drag:
objectCurrentDragging = myImage;
Then in the dragObject function, simply reference objectCurrentDragging:
objectCurrentDragging.x = ...
Hope that works out for you.
So I have finally figured it out. The key was not to add an event to the stage, but rather, to add the "releaseToDrop" function to the *MouseEvent.MOUSE_UP* event on the child element in the same function that adds it to the stage. Now I get no more errors, and it works with multiple instances of the child objects (movie clips) on the stage.
Here is the code that works:
// Allow buttons to bring objects to the stage
myButton.addEventListener(MouseEvent.CLICK, addImage);
function addImage(event:MouseEvent):void
{
var myImage:Image_mc = new Image_mc();
stage.addChild(myImage);
// Center the object
myImage.x = 300;
myImage.y = 300;
// Allow the object to be dragged
myImage.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
myImage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
function clickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
function releaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}

Removing an event listener as well as a sprite at the same time AS3

I’m having trouble removing the an event listener as well as the sprite at the same time. I currently get an error:
TypeError: Error #1009: Cannot access
a property or method of a null object
reference.
And if I comment out removeChild, I have no error but, obviously, the sprite remains on the screen. Any ideas how I can rid myself of that error?
//Bullet extends Sprite Class
bullet:Bullet = new Bullet();
mc.addChild(bullet);
bullet.addEventListener(Event.ENTER_FRAME, shoot);
function shoot(e:Event):void {
var shot:Bullet = e.currentTarget as Bullet;
//check shot is outside the frame
if (shot.x < 0 - shot.width || shot.x > stage.stageWidth || shot.y > 525)
{
//trying to remove the thing and it's listener
e.currentTarget.removeEventListener(e.type,arguments.callee);
e.currentTarget.parent.removeChild(shot);
}
else
{
shot.setInMotion();
}
}
Apart from a missing var before bullet:Bullet, I don't see anything wrong in the example code. You should set a breakpoint right after:
var shot:Bullet = e.currentTarget as Bullet;
And figure out why shot is null. I suspect there is something amiss in a piece of code outside of the little bit you're providing as the example. If the code is working with only the removeChild line commented out, it tells me that e.currentTarget is not null, but that it's also not a reference to an instance of type Bullet (i.e. the "as" cast is returning null).
Try reversing these lines
Maybe the reference to e.currentTarget is getting lost through the object references
e.currentTarget.removeEventListener(e.type,arguments.callee);
e.currentTarget.parent.removeChild(shot);
to
e.currentTarget.parent.removeChild(shot);
e.currentTarget.removeEventListener(e.type,arguments.callee);