Acces of undefined property - actionscript-3

Um im programming a little program right now, but I noticed that something wont work. When I try running my game it gives me this:
Scene 1, Layer 'Layer 2', Frame 3, Line 16 1120: Access of undefined property NumberofWins.
I tried to look for any possible reason why this code wont work, and haven't found a reason:
var Numberofwins = 0;
CN.addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(event:Event):void{
if(CN.currentFrame == 11){
CN.gotoAndPlay(1);
}
}
import flash.events.MouseEvent;
Submit.addEventListener(MouseEvent.CLICK, CheckIf8);
function CheckIf8(event:Event):void{
if(CN.currentFrame == 8) {
NumberofWins++;
}
else{
gotoAndStop(4);
}
}

You have a typo var Numberofwins = 0; should be var NumberofWins = 0;
or
NumberofWins++;should be Numberofwins++;
If you want to use the convention when naming vars, use camel case: numberOfWins. First letter lowercase except for class and constructor function names.

Related

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

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

Flash Action Script 3.0 error 1120

So I have come to a point where I have to ask a question, I've already went to 3 pages of google for hours now. I hope you guys can help me out.
I'm pretty sure i've defined var UsedSlot but it says error 1120.
i've already tried putting it on different lines, like before and after the timer. I'm just a newbie and I need this for my term project.
Here are the errors
Scene 1, Layer 'Player', Frame 1, Line 55 1120: Access of undefined property UsedSlot.
Scene 1, Layer 'Player', Frame 1, Line 62 1120: Access of undefined property UsedSlot.
Scene 1, Layer 'Player', Frame 1, Line 90 1120: Access of undefined property UsedSlot.
Scene 1, Layer 'Player', Frame 1, Line 91 1120: Access of undefined property UsedSlot.
stop();
import flash.display.MovieClip;
import flash.events.Event;
import fl.motion.MotionEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
//initialize objects
var PlayerItems:Array = [new Items_Player_Cheese(),
new Items_Player_Cheese(),
new Items_Player_Cheese(),
new Items_Player_Cheese(),
new Items_Player_Cheese(),];
//hard code test ***working***
/*PlayerItems[0].x = Guide_Test_Start.x;
PlayerItems[0].y = Guide_Test_Start.y;
stage.addChild(PlayerItems[0]);*/
///////////////////
var j:int;
var SlotUsed:Array = new Array(PlayerItems.length);
for(j = 0 ; j < SlotUsed.length; j++)
{
SlotUsed[j] = -1
trace(SlotUsed[j]);
}
for(j = 0; j < PlayerItems.length; j++)
{
PlayerItems[j].x = Guide_Test_Start.x;
PlayerItems[j].y = Guide_Test_Start.y;
}
var PlaceOccupied:Array = [false,false,false,false,false];
//check if a place is unoccupied
var myTimer:Timer = new Timer(3000);
myTimer.addEventListener(TimerEvent.TIMER, CheckVacancy);
myTimer.start();
//choose a starting location
var CVSync:int;
function CheckVacancy(e:TimerEvent)
{
for(CVSync = 0 ; CVSync < 5; CVSync++)
{
if(PlaceOccupied[CVSync] == false)
{
// Put an object
var ItemIndex = randomRange(0,PlayerItems.length - 1);
while(UsedSlot[ItemIndex] > -1)
ItemIndex = randomRange(0,PlayerItems.length - 1);
trace("Item["+ItemIndex+"] placed on ["+CVSync+"]");
PlayerItems[ItemIndex].x = StartGuide[CVSync].x;
PlayerItems[ItemIndex].y = StartGuide[CVSync].y;
UsedSlot[j] = ItemIndex;
PlaceOccupied[CVSync] = true;
trace("Vacant Slot Filled" + getTimer() + " ms");
break;
}
}
}
//Instantiate the objects
for(j = 0; j < PlayerItems.length; j++)
{
trace("Adding Player Items");
stage.addChild(PlayerItems[j]);
}
//Add Listener To determine A hit
for(j = 0; j < PlayerItems.length; j++)
{
PlayerItems[j].addEventListener(MouseEvent.MOUSE_UP, MUp);
}
function MUp(e:MouseEvent):void
{
for(j = 0; j < PlayerItems.length; j++)
{
if(PlayerItems[j].hitTestObject(MyItem[0]))
{
CVSync[UsedSlot[j]] = false;
UsedSlot[j] = -1;
PlayerItems[j].x = -100;
trace("Point by: " + MyItem[0].name.toString());
break;
}
}
}
/*cheese.addEventListener(MouseEvent.MOUSE_UP, MUp);
function MUp(e:MouseEvent):void{
if(cheese.hitTestObject(MyItem[0]))
{
trace("Cheese Eaten by: " + MyItem[0].name.toString());
cheese.x = Guide_Test_Start.x;
cheese.y = Guide_Test_Start.y;
}
}*/
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
I'm pretty sure i've defined var UsedSlot
No, you don't and that's the problem here.
The name UsedSlot appears 4 times in the snippet you posted, which are the lines that cause the 4 errors.
None of them defiens the variable.
You do however define another variable:
var SlotUsed:Array
in your code.
If this is unclear: these are two different variables. Just because you define a variable SlotUsed somewhere in your code doesn't mean you can access it via some other name, like UsedSlot.
This is case sensitive, which means SlotUsed and slotUsed are not considered to be the same.
As an advice, "being pretty sure" is usually worthless when it comes to finding problems like this. Instead, prove it by finding the declaration. If you cannot find it, it's probably not there. After all, the compiler cannot find it.
It's a common convention to start variable names with a small letter, which helps to distinguish them from names of classes (or interfaces...).
While it's your choice to code however you want, it's easier for others to read your code if you stick to common conventions and therefore simplifies the task of helping you out. =)

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

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