Flash AS3 Error: 1013: The private attribute may be used only on class property definitions - actionscript-3

C:\Users\Lab3project\MainDocument.as, Line 103, Column 7 1013: The
private attribute may be used only on class property definitions.
The lines of code concerned:
package {
//these are flash built-in classes
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.Event;
//Our own custom class
import MainTimer;
public class MainDocument extends MovieClip{
private var gameTimer:MainTimer;
private var thePlayer:Player;
private var theEnemy:Enemy;
private var maxEnemies: int = 3;
private var e:int = 0;
private var childrenOnStage:int;
public function MainDocument() {
// constructor code
trace("the main document is alive");
// new instance MainTimer class
gameTimer = new MainTimer();
// must add it to the stage
addChild(gameTimer);
//adjust its postion on stage
gameTimer.x = 20;
gameTimer.y = 20;
//add the player
thePlayer = new Player();
addChild(thePlayer);
// adjust its postion on the stage
thePlayer.x = stage.stageWidth * 0.5;
//assign the name property
thePlayer.name = "player";
while(e < maxEnemies){
createEnemy();
e++;
} //end while
//Update this variable every time a child is added to the stage
childrenOnStage = this.numChildren
// Add event listener to control timing of main game loop
addEventListener(Event.ENTER_FRAME,mainGameLoop);
}//end function MainDocument
private function createEnemy():void{
trace("create enemy");
theEnemy = new Enemy();
addChild(theEnemy);
//Place in a random spot on stage
theEnemy.x = (Match.random() * stage.stageWidth);
theEnemy.y = 0;
//assign the name property
theEnemy.name = "enemy";
//Update this variable every time a child is added to the stage
childrenOnStage = this.numChildren
} //end function createEnemy
//the main loop for the game
private function mainGameLoop(event:Event): void{
checkForGameReset();
processCollisions();
scrollStage();
} // end functiom mainGameLoop
private function checkForGameReset():void{
//define conditions
} //end function checkForGameReset
private function processCollisions():void{
//set up the main loop to look through all collidale objects on stage
for(var c:int;c < childOnStage;c++){
//trace ("Child on stage c= " + c +
//test for a player of enemy child on stage
if (getChildAt(c).name == "player" || getChildAt(c).name == "enemy"){
//see if object is touching the game stage
if( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)){
//while it is still touching the game stage
while( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)==true){
//called from CollisionObject class,so force the connection
CollisionObject(getChildAt(c)).incrementUpward();
if( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)==false){
}CollisionObject(getChildAt(c)).keepOnBoundary(); //make it stick
} // end if
} // end while
} //end if touching
} //end if player or enemy
} //end for loop
} //end function processCollisions
The lines of code concerned: is here where im getting the error
private function scrollStage(): void
{
// figure out logic
}
// end function scrollStage
//add the enemy for testing
theEnemy = new Enemy();
addChild(theEnemy);
// adjust its postion on the stage
theEnemy.x = stage.stageWidth * 0.5;
theEnemy.y = 200;
} // end public function MainDocument
} // end public class
} // end package
After I have change that code ,it then says I have a extra character at the end but the characters I have to have at the end.

Problems like this often occur because of a slight typo in syntax. Perhaps you have accidentally declared your private function within another function?
public function doSomething() {
//a whole bunch of stuff
//then we forget to close the function with a brace "}"
private function scrollStage() {
//but we're still defining doSomething! Throw error!
}
Go and look for errors like this, and hopefully you can find the problem.

Related

Test collision between two movie clips in two different classes AS3

I need to test the collision between 2 movie clips, using air for android action script 3.
Its a collision between an object and several obstacles.
My structure is the following :
The base FLA file, is linked to Action Script file called baseCode.as.
In this AS file, i create the obsctacles, using the following code :
baseCode.as :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
import Mc_MC; // Not strictly needed
public class baseCode extends flash.display.MovieClip
{
//private static var SYMBOLS:Array = new Array(MySymbol1, MySymbol2);
public var t:int = 0;
public function baseCode()
{
// Create five symbols:
for (var i:int = 0; i < 5; i++) {
trace(i);
makeSymbol();
}
}
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
public function makeSymbol():void
{
trace("IT entered makeSymbol");
// Pick a random symbol from the array:
// var symType:Class = SYMBOLS[0];
//trace(SYMBOLS[Math.random() * SYMBOLS.length]);
// Construct the new symbol:
//var Positi : Number = new Number(Math.random);
var loc:Point = new Point(randomRange(100,stage.stage.height),0);
var loc2:Point = new Point(randomRange(110,stage.stage.height),0);
//var loc:Point = new Point(10*randomRange(15, stage.width),100*randomRange(10 , stage.width));
trace("this is the starting point" , loc);
var sym:Mc_MC = new Mc_MC(1 + Math.random() *10, loc);
if( t % 2 == 0 ){
var sym2:Mc_MC2 = new Mc_MC2(15 + Math.random() *10, loc);
// Listen for the object hitting the left edge:
//sym2.addEventListener(Event.COMPLETE, remakeObject);
this.addChild(sym2);
}
sym.addEventListener(Event.COMPLETE, remakeObject);
this.addChild(sym);
t ++;
}
public function remakeObject(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, remakeObject);
//e.removeChild(sym);
//e.parent.removeChild(this.child);
// removeChild(this);
// this.removeChild(sym);
// Replace the dead symbol:
makeSymbol();
}
}
}
Mc_MC and Mc_MC2 are two Action Script file in which the obstacles are called :
Mc_MC.as :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.*;
import flash.display.Screen;
import flash.system.Capabilities;
public class Mc_MC extends MovieClip
{
public var speed:Number; // Pixels moved per frame
var valuee:baseCode;
public function Mc_MC(speed:Number, startPosition:Point)
{
this.speed = speed;
this.addEventListener(Event.ENTER_FRAME, update);
this.x = startPosition.x;
this.y = startPosition.y;
}
public function update(speed:Number)
{
var screenWidth:Number = Capabilities.screenResolutionX;
var screenHeight:Number = Capabilities.screenResolutionY;
trace("this.y" , this.y);
trace("this is the stage height" , screenHeight);
trace("this.speed" , this.speed);
if (this.y >= screenHeight - 100) { // We're at the left edge
trace("Entered if");
trace("new Starting Pos" , this.y);
this.y = stage.height;
parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, update);
this.dispatchEvent(new Event(Event.COMPLETE));
}
else this.y += this.speed;
}
}
}
In the base FLA file, i create the main object that will collide with all the obstacles created using Mc_MC and Mc_MC2. I create it using the following code :
Home.fla
import flash.events.*
//import flash.events.EventDispatcher.addEventListener()
import flash.display.DisplayObject;
//import flash.events.MouseEvent;
import flashx.textLayout.events.UpdateCompleteEvent;
import flash.display.MovieClip;
var offsetX:Number;
var offsetY:Number;
//var draggedObject:DisplayObject;
var my_obj:OriginalObject = new OriginalObject();
//left.addEventListener(MouseEvent.MOUSE_MOVE, drag);
//The speed of the scroll movement.
var scrollSpeed:uint = 2;
//This adds two instances of the movie clip onto the stage.
var s1:ScrollBg = new ScrollBg();
var s2:ScrollBg = new ScrollBg();
left.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
function mouseDown(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,myButtonClick); //while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp(e:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME,myButtonClick); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}
right.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown2);
function mouseDown2(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp2); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,stopDragging); //while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp2(e:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME,stopDragging); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp2); //remove the listener for mouse up
}
my_obj.x = stage.width / 2;
my_obj.y = stage.height - (stage.height / 3 );
stage.addChild(my_obj);
function myButtonClick(ev:Event):void
{
trace("UPPP");
if(my_obj.x > (my_obj.width*2)){
my_obj.x = my_obj.x - 10;
trace("In the limit");
}
else {
trace("out of bounds");
}
trace("myButton has been clicked.");
}
//// This function is called when the mouse button is released.
function stopDragging(ev2:Event):void
{
trace("Down");
if(my_obj.x <= right.x){
my_obj.x = my_obj.x + 10;
}
}
How can I test the collision of the moving Mc_MC / Mc_MC2 with my_obj considering that the come from different AS files?
I am new to AS, so any help would be appreciated!
If you want to be able to collision test objects from different classes / parentage, then you need to set up your project in a way that you can gain a reference to said objects.
From the looks of it, your two objects are actually in the same class already (as your main timeline code and document class code share the same scope, so what you declare in one should be available in the other).
The only thing you are missing, is a top-level reference to your obstacle/Mc_MC. As currently you assign it to a var that is scoped to the makeSymbol function (so you only have a reference to it inside that function).
Solution
In your document class (baseCode.as) , create a top-level var to hold a reference to that obstacle: (for reference, you have a top level var called t, put this line above or below that)
private var obstacle:Mc_MC;
later in your function that instantiates the new Mc_MC (makeSymbol), assign the instance to that top level var:
obstacle = new Mc_MC(1 + Math.random() *10, loc);
addChild(obstacle);
Now you can access that obstacle var anywhere else in the main timeline or document class.
if(my_obj.hitTest(obstacle)){
}
As an aside, if you have a document class, there is no point in having code on the first frame of your main timeline as that code would work the same in your document class (though it has to be contained in a function). Here is an example of where to move main timeline code:
public class Main extends MovieClip {
public function Main():void {
//listen for the added to stage event prior to do anything display oriented
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
//this is the best place to put the equivalent of timeline code (not including vars or functions, put those in the class root NOT nested here)
}
}

AS3 Update Variable Reports Null.. in a pattern

I'm making a space game in AS3 - I have an enemy class named EnemyShip.
I've already registered the Event.ENTER_FRAME and it works correctly - the problem is that my variable, rowXY of type Array, reports both null and a non-null value.. in a pattern.
How can I keep this from happening (or is there an easier way to animate the ships to move in Flash Professional?).
The pattern is as follows:
EnemyShip:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class EnemyShip extends Sprite
{
internal var id:int;
internal var rowOrder:int;
internal var rowXY:Array;
private var dirUp:Boolean = false;
public function EnemyShip()
{
// add event listeners
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void
{
moveUpAndDown();
trace(rowXY);
function moveUpAndDown():void
{
if (dirUp)
y -= C.ENEMY_SPEED;
else
y += C.ENEMY_SPEED;
}
}
private function onAddedToStage(e:Event):void
{
// get row XY
if (rowOrder == 1)
rowXY = C.ENEMY_ROW_1;
if (rowOrder == 2)
rowXY = C.ENEMY_ROW_2;
if (rowOrder == 3)
rowXY = C.ENEMY_ROW_3;
// set XY position
x = rowXY[0];
y = rowXY[1];
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
internal function destroy():void
{
rowOrder = null;
rowXY = null;
// remove event listeners
removeEventListener(Event.ENTER_FRAME, update);
// remove from display list
parent.removeChild(this);
}
}
}
C.as (Constants):
package
{
// C stands for constants
public class C
{
// ship constants
public static const FIRE_DELAY:int = 500; // milliseconds
// laser constants
public static const LASER_SPEED:int = 30;
public static const POINTS_KILL:int = 10;
// mcScore constants
public static const SCORE_LOC_X:Number = 345;
public static const SCORE_LOC_Y:Number = -120;
// enemy ship constants
public static const ENEMY_ROW_1:Array = [485, -45];
public static const ENEMY_ROW_2:Array = [485, 25];
public static const ENEMY_ROW_3:Array = [485, 95];
public static const ENEMY_SPEED:int = 5; // 5 pixels
public static const ENEMY_Y_MIN:int = -10;
public static const ENEMY_Y_MAX:int = 10;
}
}
If you're trying to animate using Flash Professional, I would take advantage of their built in Motion Tween feature. You could also animate the ship in your code by taking advantage of the Tween class.
I reproduced the behavior that you've got using this for loop ( you can get the same result by instantiating 3 objects and insert 3 others directly to the stage ) :
for(var i:int = 1; i < 7; i++){
var enemy:EnemyShip = new EnemyShip();
enemy.rowOrder = i;
addChild(enemy);
}
here we can see very clear that where i is 4, 5 or 6, rowOrder will be null which also will fire some #1009 errors for all the instances which has the rowOrder greater that 3 from this line :
x = rowXY[0];
So to avoid that, you can, for example, restrict the value of rowOrder to be between 1 and 3, like this, for example :
enemy.rowOrder = 1 + int(Math.random()*3);
you can also set that value inside the EnemyShip class itself.
...
Hope that can help.
The Solution
It was to my knowledge that the event Event.ENTER_FRAME was only passed to objects on the stage, but this is not true.
The problem was in my EnemyShip class' constructor method - the ENTER_FRAME event was being listened to by both on- and off-stage instances - the off-stage instances had not been assigned a rowXY (this happens in the event ADDED_TO_STAGE).
Old Code:
New Code:
So what's the lesson to be learned here?
Event.ENTER_FRAME happens no matter if the object is displayed or not.
In the screenshot you posted you have this code:
if(rowOrder > 3)
rowOrder = 0;
But when you check rowOrder in onAddedToStage() you don't have a case for 0 -- only 1, 2, or 3. So rowXY never gets set when rowOrder is 0.
Either change that code to set rowOrder to 1 instead of 0, or change your other code to be zero indexed.

Accessing timeline objects from a class, giving null object reference?

All late comers this question in still active a answer is not yet reached, what you might see below is a irrelevent syntax error a nice member found for me
error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Maintest_fla::MainTimeline/createPlayer()
When i'm attempted to add the instance name wall0x objects that are in the object with the instance name world, I find that I get a null object error.
Also ignore the long list of variables, not relevant.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
public class Player extends MovieClip
{
// player settings
private var _rotateSpeedMax:Number = 20;
public var _gravity:Number = .10;
// projectile gun settings
public var _bulletSpeed:Number = 4;
public var _maxDistance:Number = 200;
public var _reloadSpeed:Number = 250;//milliseconds
public var _barrelLength:Number = 20;
public var _bulletSpread:Number = 5;
// gun stuff
private var _isLoaded:Boolean = true;
private var _isFiring:Boolean = false;
private var _endX:Number;
private var _endY:Number;
private var _startX:Number;
private var _startY:Number;
private var _reloadTimer:Timer;
private var _bullets:Array = [];
// array that holds walls
public var _solidObjects:Array = [];
//
private var _player:MovieClip;
private var _dx:Number;
private var _dy:Number;
private var _pcos:Number;
private var _psin:Number;
public var _trueRotation:Number;
public function Player()
{
// constructor code //Right hereVVVthe instance name is wall0x and it's in the object world on the stage.
_solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
/*addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);*/
}
}
}
Code I'm using in frame 2 create the player and then continuously set it's chords to another objects.
stage.addEventListener(Event.ENTER_FRAME, createPlayer);
function createPlayer(e:Event):void
{
// attach player movieclip from library
// position player in center
if (character!=null&&_player!=null)
{
_player.x = character.x + 5;
_player.y = character.y + 5;
}
else if (_player ==null && world.wall01 != null)
{
var _player:Player;
_player = new Player();
// add to display list
stage.addChild(_player);
}
}
First: You have a syntax error in these two lines:
_player.x = MovieClip.(root).character.x + 5;
_player.y = MovieClip.(root).character.y + 5;
There should not be a period after MovieClip, so it should look like this:
_player.x = MovieClip(root).character.x + 5;
_player.y = MovieClip(root).character.y + 5;
Second: You are always creating a new Player every frame. In your createPlayer method, you have the following conditional:
if(character != null && _player != null) //_player is not a defined in this scope, so it will either throw an error, or always return null/undefined
You do not have a _player var defined in the scope of that frame or the scope of the createPlayer method, you have defined it inside the scope of the else statement (which makes only available in the else statement)
Move the var _player:Player to the top of your timeline code with the other frame scoped vars.
Third: you are trying to access root in your Player constructor, the problem with this, is that when the contructor runs, your Player is not yet on the display tree, so root is null until you've added player to the stage.
example:
_player = new Player(); //this will run your contructor, but root will be null
stage.addChild(_player); //after this, your Player class will now have access to root/stage/parent object
Change your Player class so it listens for being ADDED_TO_STAGE before trying access root.
public function Player()
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
// constructor code
}
private function init(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
_solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}

Flash AS3 Error: Line 48 1013: The private attribute may be used only on class property definitions

I'm a beginner of the flash as3. I'm trying to create a game but in the beginning of my game project. A error comes. This is the error --> Line 48: 1013: The private attribute may be used only on class property definitions.
I don't know what is the problem, is anyone else know what is this problem if anybody know please tell me.
This is the code:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main()
{
// variables and constants
const NUMBER_OF_TILES:uint = 20;
const TILES_PER_ROW:uint = 5;
var tiles:Array=new Array();
var tile:tile_movieclip;
// end of variables and constants
for (var i:uint=0; i<NUMBER_OF_TILES; i++)
{
tiles.push(Math.floor(i/2));
trace("My tiles: "+tiles);
// end of tiles creation loop
// end of tiles creation loop add the following code:
// shuffling loop
var swap,tmp:uint;
for (i=NUMBER_OF_TILES-1; i>0; i--)
{
swap = Math.floor(Math.random() * i);
tmp = tiles[i];
tiles[i] = tiles[swap];
tiles[swap] = tmp;
trace("My shuffled tiles: "+tiles);
// end of shuffling loop
//tile placing loop
for (i=0; i<NUMBER_OF_TILES; i++)
{
tile=new tile_movieclip();
addChild(tile);
tile.cardType = tiles[i];
tile.x=5+(tile.width+5)*(i%TILES_PER_ROW);
tile.y=5+(tile.height+5)* (Math.floor(i/TILES_PER_ROW));
tile.gotoAndStop(NUMBER_OF_TILES/2+1);
tile.buttonMode = true;
tile.addEventListener(MouseEvent.CLICK, onTileClicked);
// end of tile placing loop
}
private function onTileClicked(e:MouseEvent) {
trace("you picked a "+e.currentTarget.cardType);
e.currentTarget.gotoAndStop(e.currentTarget.cardType+1);
}
}
}
}
}
}
Your private function onTileClicked is inside your Main method. Access modifiers have to be used at the class level (the same level as your Main method), not inside other methods.
You should either remove the private modifier, or move the function to the class level. As it is, the function can't be accessed outside Main, so it doesn't need to be private.

AS3: Error 1009: Null reference

I am making a game in ActionScript 3. I have a Menu Class with a method that renders a Menu.
I make an instance of Menu in my Main class, and then call the method. When I debug the application I get a null reference error. This is the code of the menu class:
package
{
import flash.display.MovieClip;
import menucomponents.*;
public class Menu extends MovieClip
{
public function Menu()
{
super();
}
public function initMenuComponents():void{
var arrMenuButtons:Array = new Array();
var btnPlay:MovieClip = new Play();
var btnOptions:MovieClip = new Options();
var btnLikeOnFacebbook:MovieClip = new LikeOnFacebook();
var btnShareOnFacebook:MovieClip = new ShareOnFacebook()
arrMenuButtons.push(btnPlay);
arrMenuButtons.push(btnOptions);
arrMenuButtons.push(btnLikeOnFacebbook);
arrMenuButtons.push(btnShareOnFacebook);
var i:int = 0;
for each(var item in arrMenuButtons){
item.x = (stage.stageWidth / 2) - (item.width / 2);
item.y = 100 + i*50;
item.buttonMode = true;
i++;
}
}
}
}
Thanks in advance.
Your issue is likely that stage is not populated yet when your for loop runs. Try the following:
public class Main extends MovieClip {
public function Main() {
super();
var menu:Menu = new Menu();
//it's good practice - as sometimes stage actually isn't populated yet when your main constructor runs - to check, though in FlashPro i've never actually encountered this (flex/flashBuilder I have)
if(stage){
addedToStage(null);
}else{
//stage isn't ready, lets wait until it is
this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
}
}
private function addedToStage(e:Event):void {
menu.addEventListener(Event.ADDED_TO_STAGE,menuAdded); //this will ensure that stage is available in the menu instance when menuAdded is called.
stage.addChild(menu);
}
private function menuAdded(e:Event):void {
menu.initMenuComponents();
}
}