Change textfield in movieclip with loop actionscript 3 - actionscript-3

I have six movieclips on the stage and I want to build a for loop that will change the text inside of the movieclips. I have an array with all the names in it that i want to be added to each clip.
var a_letters:Array = new Array('a','b','c','d','e','f');
for(var i = 0; i <= 6; i++ ){
var tempBTN = 'btn'+i+'_mc';
this.getChildByName(tempBTN).letter_txt.html = a_letters[i-1];
}
When I run this I get this error
Scene 1, Layer 'ACTIONS', Frame 1, Line 28
1119: Access of possibly undefined property letter_txt through a reference with static type flash.display:DisplayObject.

Related

AS2 carousel gallery to AS3

I'm making carousel gallery for my class, however we got a code written in AS2 and have to transform it into AS3. I have a problem with two fragments:
// create the objects for the circular motion
for (var i:Number = 0; i < numberOfObjects; i++)
{
this.createEmptyMovieClip('object'+i, i);
this['object'+i].loadMovie('p'+(i+1)+'.jpg');
}
Where I'm adding movie clips as objects, and then I want to use them as:
// create the motion along the circular path
this.addEventListener(Event.ENTER_FRAME, function()
{
// loop over all the objects
for (var i:Number = 0; i < numberOfObjects; i++)
{
thisObj = this['object'+i];
placeObj(thisObj,i);
displayObj(thisObj);
}
})
Of course I get an exception:
Scene 1, Layer 'actions', Frame 1, Line 85 1120: Access of undefined property thisObj.
I know that createEmptyMovieClip as well as loadMovie don't work in AS3, and I probably should make MovieClip as new MovieClip, however I still have troubles how to make this work. I'll be truly grateful for any advices.
You need to apply var keyword before you introduce a new variable.
Correct your as3 code this:
var thisObj:Sprite = this['object'+i];

AS3: Call to a possibly undefined method addEventListener through a reference with static type Class

I am getting the following errors:
Scene 1, Layer 'Actions', Frame 1, Line 110, Column 14 1061: Call to a possibly undefined method addEventListener through a reference with static type Class.
Scene 1, Layer 'Actions', Frame 1, Line 113, Column 14 1061: Call to a possibly undefined method removeEventListener through a reference with static type Class.
Scene 1, Layer 'Actions', Frame 1, Line 128, Column 14 1061: Call to a possibly undefined method addEventListener through a reference with static type Class.
Scene 1,
Layer 'Actions', Frame 1, Line 131, Column 14 1061: Call to a possibly
undefined method removeEventListener through a reference with static
type Class.
the code part is:
function allowTapCaveman():void {
/*line 110*/ btn_caveman.addEventListener(TouchEvent.TOUCH_TAP, btn_cavemanMenu);
}
function cancelTapCaveman():void {
/*line 113*/ btn_caveman.removeEventListener(TouchEvent.TOUCH_TAP, btn_cavemanMenu);
}
function allowTapCavemanClose():void {
/*line 128*/ btn_caveman.addEventListener(TouchEvent.TOUCH_TAP, btn_cavemanMenuClose);
}
function cancelTapCavemanClose():void {
/*line 138*/ btn_caveman.removeEventListener(TouchEvent.TOUCH_TAP, btn_cavemanMenuClose);
}
btn_caveman is a movieclip (yeah I know I have 'btn') that gets called on the stage via an array;
var startingcaveman:Array = new Array();
for (var i:Number = 0; i<2; i++) {
startingcaveman.push(new btn_caveman());
addChild(startingcaveman[i]);
startingcaveman[i].name = 'startingcavemen' +i;
startingcaveman[i].x = Math.random()*550;
startingcaveman[i].y = Math.random()*400;
}
startingcaveman[0].x = 213.60;
startingcaveman[0].y = 312.90;
startingcaveman[1].x = 211.75;
startingcaveman[1].y = 400.15;
stage.addEventListener(Event.ENTER_FRAME, example);
function example (evt:Event) {
for each (var btn_caveman in startingcaveman) {
addEventListener(TouchEvent.TOUCH_TAP, btn_cavemanMenu3);
}
}
It's probably so simple but I can't get it which is annoying the hell outta me.
The simple answer which should allow you to fix your problem and in view of your coding style maybe get rid of many future problems as well:
You cannot use as a variable name the name of a class. In your case you have a class named btn_caveman but later on you try to use a variable named btn_caveman as well. Do not do that ever.
If you were to follow coding conventions you would never run into those type of problems. Class names should be that way:
ButtonCaveman
Variable name should be that way:
buttonCaveman
This should to it
var startingcaveman:Array = [];
for (var i:Number = 0; i<2; i++) {
// symbols should start with an upcase letter,
// because they're classes
// stick to "Caveman" for example
var btn:MovieClip = new btn_caveman();
startingcaveman.push(btn);
addChild(btn);
btn.name = 'startingcavemen' +i;
btn.x = Math.random()*550;
btn.y = Math.random()*400;
}
startingcaveman[0].x = 213.60;
startingcaveman[0].y = 312.90;
startingcaveman[1].x = 211.75;
startingcaveman[1].y = 400.15;
stage.addEventListener(Event.ENTER_FRAME, example);
function example (evt:Event) {
// here was the error. you used the class name.
// that's why naming is important!
for each (var btn in startingcaveman) {
btn.addEventListener(TouchEvent.TOUCH_TAP, btn_cavemanMenu3);
}
}

Modify stage size cause error - Flash CC

I have an application with 2 scene, the first scene contains 4 movieclips which are placed manually into the scene, every movieClip had an instance name (mc1,mc2..mc4), and I create an array with this objects var arr:Array = [mc1..mc4];
I add them an mouse event listener , for each (var i in arr){ i.addEvent...mouse.click)};
In this scene I also have an button "next scene" which have this code : nextScene();
In the second scene I have one button "back" which have this code : prevScene();
My app is 1200 x 720 px size, and I want it 800 x 600, so when I'm changing this manually .
When I'm running the application, everything is good, a go to next scene, and when I press back, it gives me an error at first scene
for each(var i in arr){
i.addEvent...mouse.click)
};
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/frame2()
at flash.display::MovieClip/prevScene()
at Main/onBack()
When I trace mc1 , at first run scene1 the output is //movieclip
when I'm pressing back the output is // null
and if I trace arr, the output is // ,movieclip,movieclip,movieclip (first only comma)
what can be the problem? thank you
Scene 1 code:
stop();
trace(mc1); // first run -> object [MovieClip]
// when back pressd -> null
var selectedIm:MovieClip = mc1;
var selectedD = d1;
var difficulty:uint = 3;
var imgs:Array = [mc1,mc2,mc3,mc4];
var diff:Array = [d1,d2,d3,d4];
goBtn.addEventListener(MouseEvent.CLICK, onGo);
for each(var i in imgs){
i.addEventListener(MouseEvent.CLICK, onImage); //here is the error, NULL OBJECT
}
function onGo(e:MouseEvent):void{ //next button
new Clk().play();
nextScene();
}
function onImage(e:MouseEvent):void{
new Clk().play();
if(selectedIm) selectedIm.filters = [];
selectedIm = e.target as MovieClip;
addOutline(selectedIm,0xFFFFFF,6);
}
...
Scene 2 code:
...
function onBack(e:MouseEvent):void{
new Clk().play();
removeChild(pz);
timer.reset();
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, numara);
prevScene();
....
}

How do you select a random colortransform from an array and applying it to a movieclip in Flash ActionScript 3?

I am trying to make a Tetris game in ActionScript 3. I am using a movieClip and colorTransform array to create colorful, random, unique pieces. Randomizing the frame in the movie clip works well enough, but when I try to apply a random color tint using the clip's colorTransform property, I get this:
Tetris.as, Line 342 1067: Implicit coercion of a value of type Number to an unrelated type flash.geom:ColorTransform.
Here is some sample code:
private function LandTetromino():void
{
var cT:int = currentTetromino;
var landed:Tetris_Shapes;
for (var i:int=0; i<shapeBuilder[cT][currentRotation].length; i++)
{
for (var j:int=0; j<shapeBuilder[cT][currentRotation][i].length; j++)
{
if (shapeBuilder[cT][currentRotation][i][j]==1)
{
landed = new Tetris_Shapes();
landed.transform.colorTransform = Math.floor(Math.random()*allcolorTransforms.length);
landed.gotoAndStop(Math.floor(Math.random()*12));
addChild(landed);
landed.name="r"+(startingRow+i)+"c"+(startingCol+j);
boardArray[startingRow+i][startingCol+j]=1;
}
}
}
removeChild(tetrisShape);
dropTime.removeEventListener(TimerEvent.TIMER, OnTimeTick);
dropTime.stop();
CheckForCompleteLines();
}
allcolorTransforms is ColorTransform Array?
if right. correct follow code.
landed.transform.colorTransform = allcolorTransforms[Math.floor(Math.random()*allcolorTransforms.length)];

converting Actionscript 2 code into Actionscript 3

Recently I followed and made the 3d carousel in AS2, but I'm looking to use it and make it in AS3. Is there any possible way of converting the code so the carousel can work in AS3?
Below is the code for the AS2 carousel:
import mx.utils.Delegate;
var numOfItems:Number;
var radiusX:Number = 300;
var radiusY:Number = 75;
var centerX:Number = Stage.width / 2;
var centerY:Number = Stage.height / 2;
var speed:Number = 0.05;
var perspective:Number = 130;
var home:MovieClip = this;
var tooltip:MovieClip = this.attachMovie("tooltip","tooltip",10000);
tooltip._alpha = 0;
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function()
{
var nodes = this.firstChild.childNodes;
numOfItems = nodes.length;
for(var i=0;i<numOfItems;i++)
{
var t = home.attachMovie("item","item"+i,i+1);
t.angle = i * ((Math.PI*2)/numOfItems);
t.onEnterFrame = mover;
t.toolText = nodes[i].attributes.tooltip;
t.icon.inner.loadMovie(nodes[i].attributes.image);
t.r.inner.loadMovie(nodes[i].attributes.image);
t.icon.onRollOver = over;
t.icon.onRollOut = out;
t.icon.onRelease = released;
}
}
function over()
{
home.tooltip.tipText.text = this._parent.toolText;
home.tooltip._x = this._parent._x;
home.tooltip._y = this._parent._y - this._parent._height/2;
home.tooltip.onEnterFrame = Delegate.create(this,moveTip);
home.tooltip._alpha = 100;
}
function out()
{
delete home.tooltip.onEnterFrame;
home.tooltip._alpha = 0;
}
function released()
{
trace(this._parent.toolText);
}
function moveTip()
{
home.tooltip._x = this._parent._x;
home.tooltip._y = this._parent._y - this._parent._height/2;
}
xml.load("icons.xml");
function mover()
{
this._x = Math.cos(this.angle) * radiusX + centerX;
this._y = Math.sin(this.angle) * radiusY + centerY;
var s = (this._y - perspective) /(centerY+radiusY-perspective);
this._xscale = this._yscale = s*100;
this.angle += this._parent.speed;
this.swapDepths(Math.round(this._xscale) + 100);
}
this.onMouseMove = function()
{
speed = (this._xmouse-centerX)/2500;
}
When I add this code in AS3 I get the following error:
Scene 1, Layer 'Layer 1', Frame 1, Line 1 1172: Definition mx.utils:Delegate could not be found.
Scene 1, Layer 'Layer 1', Frame 1, Line 1 1172: Definition mx.utils:Delegate could not be found.
Scene 1, Layer 'Layer 1', Frame 1, Line 41 1120: Access of undefined property Delegate.
Scene 1, Layer 'Layer 1', Frame 1, Line 6 1119: Access of possibly undefined property width through a reference with static type Class.
Scene 1, Layer 'Layer 1', Frame 1, Line 7 1119: Access of possibly undefined property height through a reference with static type Class.
I'm quite new to AS2 and AS3 but after some research I understand that import mx.utils.Delegate; is no longer need in AS3 as it already has delegate and they are already built in so in the code so I delete the delegate which are line 1 and line 41 and got two errors:
Scene 1, Layer 'Layer 1', Frame 1, Line 6 1119: Access of possibly undefined property width through a reference with static type Class.
Scene 1, Layer 'Layer 1', Frame 1, Line 7 1119: Access of possibly undefined property height through a reference with static type Class.
Now I cant figure out what to do so can someone help me convert this code from AS2 to AS3?
You have quite a few things to address here:
Your mouse events need to be changed to as3 calls
t.icon.onRollOver = over, in as3 looks more like t.icon.addEventListener(MouseEvent.ROLL_OVER, over);
attachMovie is no longer used in as3.
you need to export for actionscript the movie you want to get from the library with a unique class name, then use new someName(); to create it. Then it must be added to the display list with addChild
onEnterFrame is not used in as3, you need to create an enterframe event more like this: **addEventListener(Event.ENTER_FRAME, someFunction);
delegate is not used in as3.
flags on _x, _y, _parent, _alpha etc have been removed in as3. just use x,y, parent, alpha etc.
swapDepths has been removed from as3, You need to use the display list to add/remove/swap levels.
sounds like you might need to study up a little on as3 before you can properly tackle this one! try checking out this link for comparisons between as2 and as3 functionality.
http://www.actionscriptcheatsheet.com/downloads/as3cs_migration.pdf