converting Actionscript 2 code into Actionscript 3 - 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

Related

Flash Actionscript3 - error 1120

I wanted to ask for some help on an error I have in Flash Professional while using AS3.I keep getting a couple of errors and I am not sure how to correct them. Here are the errors below (5 in total):
Scene 1, Layer 'Content', Frame 4, Line 25, Column 3 1120: Access of undefined property grid.
Scene 1, Layer 'Content', Frame 4, Line 8, Column 1 1120: Access of undefined property grid.
Scene 1, Layer 'Content', Frame 4, Line 9, Column 1 1120: Access of undefined property grid.
Scene 1, Layer 'Content', Frame 4, Line 10, Column 1 1120: Access of undefined property grid.
Scene 1, Layer 'Content', Frame 4, Line 11, Column 1 1120: Access of undefined property grid.
Here is my code for this page, I basically wanna place parts of the XML document into a datagrid:
stop();
//Flash generated imports
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
//Datagrid added from components menu
grid.addColumn("Title");
grid.addColumn("Genre");
grid.addColumn("Runtime");
grid.addColumn("AgeRating");
var loader:URLLoader = new URLLoader(new URLRequest("movieList.xml"));
loader.addEventListener(Event.COMPLETE, onLoaded);
//Added a loop to handle the multiple movie instances
function onLoaded(e:Event):void
{
var xml:XML = new XML(loader.data);
var itemList:XMLList = xml..movie_info;
var len:int = itemList.length();
for(var i:int=0; i<len; i++)
{
grid.addItem({Title:itemList[i].Title,
Genre:itemList[i].Genre,
Runtime:itemList[i].Runtime,
AgeRating:itemList[i].AgeRating});
}
}
Any help would be greatly appreciated :)

ActionScript3:Undefined property error

I'm making a toggle button to resume/pause the audio in Adobe flash CS 5:
I used a Code Snippet "click to play/stop Sound".
Here is the code:
pause_play_button.addEventListener(MouseEvent.CLICK,fl_ClickToPlayStopSound_2);
var fl_ToPlay_2:Boolean = true;
var resumeTime:Number = 0.00;
var s:Sound = new Tanishma_Sound();
var fl_SC_2:SoundChannel ;
function fl_ClickToPlayStopSound_2(evt:MouseEvent):void
{
if(fl_ToPlay_2)
{
f1_SC_2 = s.play (resumeTime);
}
else
{
resumeTime = f1_SC_2.position;
f1_SC_2.stop ();
}
fl_ToPlay_2 = !fl_ToPlay_2;
}
I have this error and I don't know how to fix it:
Scene 1, Layer 'Actions', Frame 1, Line 47 1120: Access of undefined property f1_SC_2.
Any Help!
That error means that Flash can't find something you've referenced. In your case, this is because of a syntax typo.
You have defined: (note the f then the letter l)
var fl_SC_2:SoundChannel;
Yet later on, you've change the 'l' to the numeral '1' in three places.
f1_SC_2
Should be:
if(fl_ToPlay_2)
{
fl_SC_2 = s.play (resumeTime);
}
else
{
resumeTime = fl_SC_2.position;
fl_SC_2.stop ();
}

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);
}
}

Change textfield in movieclip with loop 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.

AS3 Bouncing Ball

Okay, after learning tutorials online, I'm trying to make a bouncing ball in AS3.
Here's my code thus far:
var count:Number = 0;
var bounceHeight:Number = 100;
var floorHeight:Number = 300;
var speed:Number = .1;
function run(e:Event):void
{
ball_mc.y = floorHeight - Math.abs(Math.cos(count)) * bounceHeight;
count += speed;
}
{
this.addEventListener(Event.ENTER_FRAME(run));
}
Thanks in advance for the help!
EDIT: The compiler errors are
Scene 1, Layer 'Layer 1', Frame 1, Line 13 1195: Attempted access of
inaccessible method ENTER_FRAME through a reference with static type
Class.
Scene 1, Layer 'Layer 1', Frame 1, Line 13 1136: Incorrect
number of arguments. Expected 2.
Within your closure, addEventListener requires a type parameter and a listener function.
Your type is Event.ENTER_FRAME and your handler is run, which means to call run every frame you need:
addEventListener(Event.ENTER_FRAME, run);
Therefore, your code should be:
function run(e:Event):void
{
ball_mc.y = floorHeight - Math.abs(Math.cos(count)) * bounceHeight;
count += speed;
}
this.addEventListener(Event.ENTER_FRAME, run);