The supplied DisplayObject must be a child of the caller DisplayObjectContainer/setChildIndex() - actionscript-3

I don't know where is the problem
I will very thankful for the person who will explain to me .
I need any kind person to answer me .This is my whole code nothing else .The problem which i faced is
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/setChildIndex()
at CreateSimpleDynamicRolloverswithAs3_fla::MainTimeline/rolloverF()
even I make this code as comment the error still persists
images_mc.setChildIndex(evt.currentTarget as MovieClip, numChildren-1);
import fl.transitions.easing.*;
import flash.display.Shape;
import fl.transitions.Tween;
import flash.events.MouseEvent;
import flash.display.MovieClip;
var imgBorder:Shape = new Shape();
imgBorder.graphics.lineStyle(5,0xffffff,1,true,"normal");
imgBorder.graphics.drawRect(0,0,250,120);
addChild(imgBorder);
imgBorder.visible = false;
images_mc.addEventListener(MouseEvent.MOUSE_OVER , rolloverF);
images_mc.addEventListener(MouseEvent.MOUSE_OUT , rolloutF);
function rolloverF(evt:MouseEvent):void{
imgBorder.visible = true;
var borderTween:Tween = new Tween(imgBorder,"alpha",Strong.easeOut,0,1,1,true);
var borderW:Tween = new Tween(imgBorder,"width",Strong.easeOut,255,280,1,true);
var borderH:Tween = new Tween(imgBorder,"height",Strong.easeOut,125,137,1,true);
var imgW:Tween = new Tween(evt.currentTarget,"width",Strong.easeOut,255,280,1,true);
var imgH:Tween = new Tween(evt.currentTarget,"height",Strong.easeOut,125,137,1,true);
trace (images_mc.mc1.name);
imgBorder.x = evt.currentTarget.x;
imgBorder.y = evt.currentTarget.y;
//even i make this code as comment error sitll persists
images_mc.setChildIndex(evt.currentTarget as MovieClip, numChildren-1);
//trace(images_mc.mc1.name);
}
function rolloutF(evt:MouseEvent):void{
var borderTweenBack:Tween = new Tween(imgBorder,"alpha",Strong.easeOut,1,0,1,true);
var borderWBack:Tween = new Tween(imgBorder,"width",Strong.easeOut,280,255,1,true);
var borderHBack:Tween = new Tween(imgBorder,"height",Strong.easeOut,137,125,1,true);
var imgWBack:Tween = new Tween(evt.currentTarget,"width",Strong.easeOut,275,250,1,true);
var imgHBack:Tween = new Tween(evt.currentTarget,"height",Strong.easeOut,132,120,1,true);
}

This is a logic problem. You add rolloverF listener to images_mc so images_mc will be the currentTarget. Next you want images_mc to set the index of the currentTarget which is impossible since the currentTarget is images_mc itself. Maybe you mean evt.target?
Also setChildIndex(evt.currentTarget as MovieClip, numChildren-1) or addChildAt(evt.currentTarget as MovieClip, numChildren-1) both translate to a simple addChild().

Related

Why isn't my box2D(flash) object moving correctly under gravity?

I am creating a world in flash using box2d. I have done this before and I am sure it is something simple but I cannot identify it.
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2Body;
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2Fixture;
import flash.events.TimerEvent;
import flash.utils.Timer;
//variables
var startVelocity:b2Vec2 = new b2Vec2(50, 0);
//world defintion
var gravity:b2Vec2 = new b2Vec2(0,10);
var world:b2World = new b2World(gravity, true);
//object physics object
var objectBodyDef:b2BodyDef = new b2BodyDef();
objectBodyDef.type = b2Body.b2_dynamicBody;
var objectBody:b2Body = world.CreateBody(objectBodyDef);
objectBody.SetLinearVelocity(startVelocity);
//object shape definition. Change to box later
var objectShape:b2CircleShape = new b2CircleShape(5);
var objectFixtureDef:b2FixtureDef = new b2FixtureDef();
objectFixtureDef.shape = objectShape;
var objectFixture:b2Fixture = objectBody.CreateFixture(objectFixtureDef);
//outlines for objects
graphics.lineStyle(3, 0xff0000);
//Create world timing
var stepTimer:Timer = new Timer(0.025 * 1000);
stepTimer.addEventListener(TimerEvent.TIMER, onTick);
stepTimer.start();
function onTick(a_event:TimerEvent):void{
graphics.moveTo(objectBody.GetPosition().x, objectBody.GetPosition().y);
world.Step(0.25, 10, 10);
graphics.lineTo(objectBody.GetPosition().x, objectBody.GetPosition().y);
trace("objectBody x: " + objectBody.GetPosition().x +" objectBody y: " + objectBody.GetPosition().y);
}
I am doing this from the timeline so The issue is not anything to do with document class.
The above code works fine for the most part however the "object" does not move in a parabola as one would expect. It stops moving right when x is about 12. This happens regardless of what value I use fore the x component of the starting velocity vector.
What am I doing incorrectly?

For loops, arrays and movie clips - how to achieve a dynamic system

I am working on a Flash scene that reads from an XML file to "build" up an animation itself.
Reading the XML is no problem, that works like a charm. My issue is when I come to placing the assets (images) on to the stage.
My code is below:
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.MovieClip;
var xmlLoader:URLLoader;
var builderXml:XML;
var container:MovieClip = new MovieClip();
var assetsArray:Array = new Array();
var bg:Sprite;
stage.addChild(container);
init();
function init():void
{
xmlLoader = new URLLoader();
xmlLoader.load(new URLRequest("build_me.xml"));
xmlLoader.addEventListener(Event.COMPLETE, processXML);
}
function processXML(e:Event):void {
builderXml = new XML(e.target.data);
for (var i:int = 0; i < builderXml.assets.*.length(); i++){
var image:MovieClip = new MovieClip();
var assetArray:Array = new Array();
image.x = builderXml.assets.asset[i].start.position.x;
image.y = builderXml.assets.asset[i].start.position.y;
trace(image.x);
assetArray.push(builderXml.assets.asset[i].source);
assetArray.push(builderXml.assets.asset[i].start.scale);
assetArray.push(builderXml.assets.asset[i].start.position.x);
assetArray.push(builderXml.assets.asset[i].start.position.y);
assetArray.push(builderXml.assets.asset[i].start.rotation);
assetArray.push(image);
assetsArray.push(assetArray);
var lc:LoaderContext = new LoaderContext();
lc.checkPolicyFile = false;
var loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
var _myURLRequest = new URLRequest(builderXml.assets.asset[i].source);
loader.load(_myURLRequest, lc);
function onImageLoaded(e:Event):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageLoaded);
image.addChild(e.target.content);
}
container.addChild(assetsArray[i][5]);
}
trace(assetsArray);
}
My XML has 2 assets listed, one 1280 x 720 image for a backdrop and the other is a simple logo that I want to position, using set x and y coordinates.
The problem is that both assets are being added to the same movieclip, despite the fact I am creating a new MC instance inside the FOR loop.
How can I get the assets to adhere to separate movieclips that I can then store in the array (pretty sure I am storing the current MC properly in the array, just happens that the MC contains 2 images, not 1 a piece)
Also, why is it that I cannot access the variable "i" inside the "onImageLoaded" function? It sits inside the FOR loop...
You are using a global variable inside a listener, and expect it to not being changed when the listener would actually fire. Listeners are asynchronous, so you should track which of the loaders fired a Event.COMPLETE event so that you could retrieve a correct instance of image MC out of those prepared at the XML parsing step, and only then stuff the loader's content inside it. This my answer has a method of doing just that, the method you should use is similar to the one that's used to retrieve a corresponding progress bar over there.

Error #1009: Cannot access a property or method of a null object reference

Working on this Flash AS3 application and I am keep getting this error when I try to make an imgLoader clickable.
The imgLoader is a dynamic loader which will load an image from XML file and its created using ActionScript.
This is the full error I get:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at apptest_fla::MainTimeline/frame1()[apptest_fla.MainTimeline::frame1:65]
at runtime::ContentPlayer/loadInitialContent()
at runtime::ContentPlayer/playRawContent()
at runtime::ContentPlayer/playContent()
at runtime::AppRunner/run()
at ADLAppEntry/run()
at global/runtime::ADLEntry()
and this is the code for making the imgLoader clickable:
imgLoader.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
anyone knows why this is happening?
EDIT
This is my entire code:
stop();
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.filters.DropShadowFilter;
var xmlLoader11:URLLoader;
var xml11:XML;
var uRequest11 = new URLRequest("my.xml");
xmlLoader11 = new URLLoader(uRequest11);
xmlLoader11.addEventListener(Event.COMPLETE, onXMLLoad11);
var imgLoader11:Loader;
var nameLoader11:Loader;
var myString11:String = 'loading';
function onXMLLoad11(e:Event) {
xml11 = new XML(e.target.data);
imgLoader11 = new Loader();
imgLoader11.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded11);
imgLoader11.load(new URLRequest(xml11.Data.Image.text()[0]));
Nametxt11.text = "" + xml11.Data.Name.text()[0];
}
function onImgLoaded11(e:Event) {
addChild(imgLoader11);
imgLoader11.height = 300;
imgLoader11.width = 300;
var bitmapContent11:Bitmap = Bitmap( e.target.content );
bitmapContent11.smoothing = true;
addChild( bitmapContent11 );
bitmapContent11.height = 150;
bitmapContent11.width = 150;
bitmapContent11.y = 65;
bitmapContent11.x = 85;
}
imgLoader11.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
Does it break when this is called:
imgLoader.addEventListener(MouseEvent.CLICK, doSomething);
or when this is called:
nextFrame()
In the first case, imgLoader is null. In the second case, something you're trying to acess fields or methods of right after nextFrame() is called is null.
EDIT:
Try moving this:
imgLoader11.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
}
to the bottom of onXMLLoad11().
function onXMLLoad11(e:Event) {
xml11 = new XML(e.target.data);
imgLoader11 = new Loader();
imgLoader11.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded11);
imgLoader11.load(new URLRequest(xml11.Data.Image.text()[0]));
Nametxt11.text = "" + xml11.Data.Name.text()[0];
imgLoader11.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
}
}

AS3 Form Inquiry

I am relatively new to AS3. I am having a few code issues at the moment i am hoping for some advice on this forum.
Basically, i am creating a form for my website. I have one combobox and I want The user enter their email address and password into the fields.
Then click on the submit button to sign in and to validate what they entered is correct. When i run my code in real-time i get an error
1120: Access of undefined property status_Txt. I dont know what status_Txt means.
I copied a large section of this code from a tutorial i read. My code is below: Can someone please tell me how i can resolve this problem.
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.MouseEvent;
import fl.data.DataProvider;
import flash.events.Event;
//Building the variables
var phpVars:URLVariables = new URLVariables();
//Requesting the php file
var phpFileRequest:URLRequest = new URLRequest("http://www.example.com");
phpFileRequest.method = URLRequestMethod.POST;
phpFileRequest.data = phpVars;
//Building the loader
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
//Variables ready for processing
phpVars.email = txt_input.text;
phpVars.p_swd = pass.text;
phpLoader.load(phpFileRequest);
btn_one.addEventListener(MouseEvent.CLICK, btnHandler);
function btnHandler(event:MouseEvent):void{
trace("Login success");
}
//Validate form fileds
function showResult(event:Event):void{
status_Txt.text = "" + event.target.data.systemResult;
trace(event.target.data.systemResult);
}
var cbox:Array = new Array();
boy[0] = "Male";
girl[1] = "Female";
c_one.dataProvider = new DataProvider(cbox);
c_one.addEventListener(Event.CHANGE, dataHandler);
function dataHandler(e:Event):void{
trace(e.target.value);
}
}
You're attempting to access an object that doesn't exist. status_Txt is being referenced in:
function showResult(event:Event):void{
status_Txt.text = "" + event.target.data.systemResult; //Either remove this or add a textfield to the stage with the instance name status_Txt
trace(event.target.data.systemResult);
}

ActionScript3: Changing button text

I have a button instance named Button that I have in my movie, this instance has a Dynamic Text object in it named myText, how can I change the text? I already tried Button.myText.text = "Stuff";
I get the error "Scene 1, Layer 'Layer 1', Frame 1, Line 7 1119: Access of possibly undefined property myText through a reference with static type flash.display:SimpleButton." When I compile.
AS:
import flash.events.MouseEvent;
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void{
TheButton.myText.text = "Moo";
}
You can't use the dot syntax to access a display object container's child display objects in AS3 as you did in AS2. Normally you would use the display object container's getChildByName() method to get its child display objects, but because your dealing with an instance of SimpleButton which is a subclass of DisplayObject that method doesn't exist. The simple solution is to change you button from a button to a movieclip after which the following should work:
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void
{
TheButton.getChildByName("myText").text = "Moo";
}
Note: the TextField display object in the TheButton display object container must have an instance name of "myText" and obviously the TheButton display object container must have an instance name of "TheButton".
Also if your going with this approach you may want to rewrite the code as follows:
import flash.display.DisplayObjectContainer
import flash.events.MouseEvent;
import flash.text.TextField;
button.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(e:MouseEvent):void
{
var button:DisplayObjectContainer = DisplayObjectContainer(e.target);
var textField:TextField = TextField(button.getChildByName("textField"));
textField.text = "Moo";
}// end function
[UPDATE]
Another solution is to create a movieclip/sprite object for the SimpleButton object's upState, overState and downState properties like the following:
import flash.display.DisplayObjectContainer;
import flash.display.SimpleButton;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
var simpleButton:SimpleButton = new SimpleButton();
addChild(simpleButton);
var content:Sprite = new Sprite();
draw(content.graphics, 0xFF0000);
var textField:TextField = new TextField();
textField.name = "textField";
textField.text = "UP";
content.addChild(textField);
simpleButton.upState = content;
simpleButton.overState = content;
simpleButton.downState = content;
simpleButton.hitTestState = content;
simpleButton.addEventListener(MouseEvent.MOUSE_OVER, onSimpleButtonMouseOver);
simpleButton.addEventListener(MouseEvent.MOUSE_DOWN, onSimpleButtonMouseDown);
simpleButton.addEventListener(MouseEvent.MOUSE_OUT, onSimpleButtonMouseOut);
simpleButton.addEventListener(MouseEvent.MOUSE_UP, onSimpleButtonMouseUp);
function onSimpleButtonMouseOver(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).overState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "OVER";
draw(content.graphics, 0xC8C8C8);
}// end function
function onSimpleButtonMouseDown(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).downState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "DOWN";
draw(content.graphics, 0x646464);
}// end function
function onSimpleButtonMouseUp(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).overState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "OVER";
draw(content.graphics, 0xC8C8C8);
}// end function
function onSimpleButtonMouseOut(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).upState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "UP";
draw(content.graphics, 0xFF0000);
}// end function
function draw(graphics:Graphics, color:uint):void
{
graphics.clear();
graphics.beginFill(color)
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
}// end function
It's extremely easy.
Suppose I have a button that was created from the Components menu called myBtn.
myBtn.label = "Awesome text";
You use label instead of creating a textfield over it then setting the texfield's text.
That's it!
In the absence of extra information as to whether you're getting errors and what they are, my first assumption would be that the system is having trouble with the variable name "Button". There is already a class named "Button", so I would think the system is thinking you are trying to call a class-level method/var of class Button instead of accessing a variable named "Button". I would try changing the name of the button var to something unique.
Children of SimpleButton are inaccessible from Actionscript; notice that it does not actually extend DisplayObjectContainer. The SimpleButton class is a bit of a mutant.
You will have to use a container MovieClip and put the SimpleButton and the label TextField inside. Alternatively you can roll your own button out of a MovieClip or use a component library. SimpleButton is not meant for this kind of use.