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

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.

Related

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

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.

Concentration game - tiles rotation

I have a simple concentration game 5x4 tiles (I have one symbol (tile_movieclip) with 11 frames - 10 frames with numbers 1-10 and 11th background(shirt cards)).
I want that at first will be visible number and after few second tiles must turn to background (11th frame). In my code I have at first visible all number and for 1 second turns just one (the lower right corner) tile. Please help.
P.S. Find //Error can be HERE!!!!! - I think problem is hiding there, but I'm not sure.
Here the source:
package {
// importing classes
import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
// end of importing classes
public class Main extends Sprite {
private var pickedTiles:Array = new Array();
private const NUMBER_OF_TILES:uint=20;
private var pause_game:Timer;
private var canPick:Boolean=true;
private var tiles:Array = new Array();
private var tile:tile_movieclip;
private var i:uint = 0;
public function Main() {
// variables and constants
// no more NUMBER_OF_TILES here
const TILES_PER_ROW:uint=5;
// end of variables and constants
// tiles creation loop
for (i=0; i<NUMBER_OF_TILES; i++) {
tiles.push(Math.floor(i/2));
}
trace("My tiles: "+tiles);
// end of tiles creation loop
// 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(tiles[i]+1); // This shows all number at first
canPick = false;
tile.buttonMode=true;
tile.addEventListener(MouseEvent.CLICK,onTileClicked);
}
pause_game=new Timer(1000,1);
pause_game.start();
pause_game.addEventListener(TimerEvent.TIMER_COMPLETE,hideTiles);
}
// end of tile_placing_loop
//Error can be HERE!!!!!
private function hideTiles(e:TimerEvent) {
pause_game.removeEventListener(TimerEvent.TIMER_COMPLETE,hideTiles);
tile.gotoAndStop(NUMBER_OF_TILES/2+1); //This turns to background just one tile (the lower right corner)
canPick = true;
}
private function onTileClicked(e:MouseEvent) {
if(canPick){
var picked:tile_movieclip=e.currentTarget as tile_movieclip;
trace("you picked a "+e.currentTarget.cardType);
// checking if the current tile has already been picked
if (pickedTiles.indexOf(picked)==-1) {
pickedTiles.push(picked);
picked.gotoAndStop(picked.cardType+1);
}
// end checking if the current tile has already been picked
// checking if we picked 2 tiles
if (pickedTiles.length==2) {
canPick = false;
pause_game=new Timer(1000,1);
pause_game.start();
if (pickedTiles[0].cardType==pickedTiles[1].cardType) {
// tiles match!!
trace("tiles match!!!!");
pause_game.addEventListener(TimerEvent.TIMER_COMPLETE,removeTiles);
} else {
// tiles do not match
trace("tiles do not match");
pause_game.addEventListener(TimerEvent.TIMER_COMPLETE,resetTiles);
}
// no more pickedTiles = new Array();
}
// end checking if we picked 2 tiles
}
}
private function removeTiles(e:TimerEvent) {
pause_game.removeEventListener(TimerEvent.TIMER_COMPLETE,removeTiles);
pickedTiles[0].removeEventListener(MouseEvent.CLICK,onTileClicked);
pickedTiles[1].removeEventListener(MouseEvent.CLICK,onTileClicked);
removeChild(pickedTiles[0]);
removeChild(pickedTiles[1]);
pickedTiles = new Array();
canPick = true;
}
private function resetTiles(e:TimerEvent) {
pause_game.removeEventListener(TimerEvent.TIMER_COMPLETE,resetTiles);
pickedTiles[0].gotoAndStop(NUMBER_OF_TILES/2+1);
pickedTiles[1].gotoAndStop(NUMBER_OF_TILES/2+1);
pickedTiles = new Array();
canPick = true;
}
}
}
You have an array of tiles, while in your listener you refer to tile, which is a single tile. You need to swap all the tiles you added to your display list, for that you can use the following construction:
for (var i:int=numChildren-1;i>=0;i--) {
var mc:DisplayObject=this.getChildAt(i);
var picked:tile_movieclip=mc as tile_movieclip;
if (picked) picked.gotoAndStop(NUMBER_OF_TILES/2+1); // now, show the back
}
This should replace your tile.gotoAndStop(NUMBER_OF_TILES/2+1); statement in hideTiles function.

AS3 Creating an array of objects

I would like to add a bunch of cars to the stage, and store them in an array as objects. The problem is I hate using external AS files and would like to keep it as simple as possible.
I tried doing :
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
var test:Array = new Array()
for (var i:Number=0; i<10; i++) {
test.push(car)
}
The problem is if I try to set a value of one object in the like
test[1].carscale = 5
Every object in the array gets their attribute carscale set to 5.
Is there any way I can do this without using external class files?
While you should use external AS files (its a good practice), here's the reason why you are having the issue, and I'm going to explain line-by-line
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
//This creates an object called car. Suppose it saves it in memory at "location" 0x12345
var test:Array = new Array();
//This creates an empty array
for (var i:Number=0; i<10; i++) {
test.push(car);
//This adds the object "car" to the array
//Since Object is a reference type, its memory location is actually added to the array
//This means you added 0x12345 to the array (10 times over the loop)
}
//The array now contains
[0x12345, 0x12345, 0x12345, .......];
//So now
test[1]; //returns the object at 0x12345
test[1].carscale=5; //sets the carscale property of the object at 0x12345
Since all objects in the array point to the same location, getting any of them will actually return the same object. This means that all of them will show carscale as 5
A solution to this would be:
var test:Array = new Array();
for (var i:Number=0; i<10; i++) {
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
test.push(car);
}
A better, REAL Object oriented solution would be to create a class called Car and then instead of doing
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
you use
var car:Car = new Car();
The Car.as class would be like this:
public class Car {
public function Car() {
//this is the constructor, initialize the object here
//Suppose the default values of the car are as follows:
carcolor="red";
carscale=5;
carpower=1000;
}
public var carcolor:String;
public var carscale:Number, carpower:Number;
}
In fact, you could even use another constructor that automatically sets the properties based on arguments:
public function Car(_color:String, _scale:Number, _power:Number) {
carcolor=_color;
carscale=_scale;
carpower=_power;
}
and call it as
var car:Car=new Car("red", 5, 1000);
In fact, the car before carcolor, carscale and carpower is not even necessary because it is obvious when you put them in a class called Car.
Like TheDarkIn1978 said you're pushing a reference of your car instance into your array. When you change the value of one instance's property the same happens for each reference.
The simple answer is to create a new object upon each interation of your for loop like in the following:
var test:Array = [];
for (var i:Number = 0; i < 10; i++)
{
var car:Object = {carcolor:String, carscale:Number, carpower:Number};
test.push(car);
}// end for
[UPDATE]
I know you said that you didn't want to use "external classes" but there are advantages to using a custom class object to store values as opposed to a Object object. Here is an example:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var cars:Vector.<Car> = new Vector.<Car>();
cars.push(new Car("red", 1, 1));
cars.push(new Car("blue", 2, 2));
cars.push(new Car("green", 3, 3));
trace(cars[2].color); // output: green
}// end function
}// class
}// end package
internal class Car
{
private var _color:String;
private var _scale:Number;
private var _power:Number;
public function get color():String { return color; }
public function get scale():String { return scale; }
public function get power():String { return power; }
public function Car(color:String, scale:Number, power:Number)
{
_color = color;
_scale = scale;
_power = power;
}// end function
}// end class
This is a good example of creating an object for the sole purpose of storing values that never change by only allowing the object's properties to be set upon initiation and using getter methods to make the values read only.
I feel dumb, I found the answer here :
http://board.flashkit.com/board/showthread.php?t=792345
You're pushing the Object reference to the array, not a unique Object each time. You have to do something like:
for(var temp=0;temp<100;temp++){
var roomData:Object=new Object;
roomData.first_time=true;
rooms.push(roomData);
}
you're adding the same object to the array multiple times. you need to create new instances of your car object.
EDIT:
although it would be a best practice to create your own "Car" class and create new instances of it, even if it's only a small object with 3 properties, here's a quick example that should get you started.
package
{
//Imports
import flash.display.Sprite;
//Class
public class Main extends Sprite
{
//Constants
private static const DEFAULT_CAR_COLOR:Number = 0x000000;
private static const DEFAULT_CAR_SCALE:Number = 1.0;
private static const DEFAULT_CAR_POWER:int = 50;
//Properties
private var carsArray:Array;
//Constructor
public function Main():void
{
init();
outputCarColors();
}
//Initialize
private function init():void
{
carsArray = new Array();
for (var i:int = 0; i < 10; i++)
{
carsArray.push(CreateCar(Math.random() * 0xFFFFFF));
}
}
//Output Car Colors
private function outputCarColors():void
{
for (var i:int = 0; i < carsArray.length; i++)
{
trace("Color of car " + i + " : " + carsArray[i].carColor);
}
}
//Create Car Object
private function CreateCar(carColor:Number = DEFAULT_CAR_COLOR, carScale:Number = DEFAULT_CAR_SCALE, carPower:int = DEFAULT_CAR_POWER):Object
{
var result:Object = new Object();
result.carColor = carColor;
result.carScale = carScale;
result.carPower = carPower;
return result;
}
}
}

AS 3.0 Dynamic Instance Names

Hi i made a custom class where i would like to create x instances of a movieclip. But the following doesn't work:
package {
import flash.display.MovieClip;
public class CustomClass extends MovieClip {
public function CustomClass(amount:uint) {
var Collector:Array = new Array();
//Add and position Tiles to stage.
for (var i:uint = 1; i <= amount; i++){
var newMovieClip:MovieClip = new MovieClip;
newMovieClip.y = amount * 10;
Collector.push(newMovieClip);
}
addChild(Collector);
}
}
}
I would like to position them on the timeline with
var customClass_mc:CustomClass = new CustomClass(10);
addChild(customClass_mc);
//try to trace the x position of one of the instances.
trace(customClass_mc.Collector[5].x);
I keep getting the error: Scene 1, Layer 'Layer 1', Frame 1, Line 5 1119: Access of possibly undefined property Collector through a reference with static type CustomClass.
Firstly, you need to declare Collector as public:
public var Collector:Array = new Array();
Your Collector is an array, not a display object, and so it can't be added to the display tree. Instead you would push each newMovieClip onto the display of Custom class and position them inside your for loop. Then you don't need the collector at all, because you can target the movieclips using getChildAt():
trace(customClass_mc.getChildAt(5).x);
I found another answer myself which i think is even better!
You don't need the container at all.
when you use the following
package {
import flash.display.MovieClip;
public class CustomClass extends MovieClip {
public function CustomClass(amount:uint) {
//Add and position Tiles to stage.
for (var i:uint = 1; i <= amount; i++){
var newMovieClip:MovieClip = new MovieClip;
newMovieClip.y = amount * 10;
newMovieClip.name = "clip"+i;
addChild(newMovieClip);
}
}
}
}
No i can acces the movieclips by using:
var customClass_mc:CustomClass = new CustomClass(10);
addChild(customClass_mc);
//try to trace the x position of the fifth instance.
trace(customClass_mc.getChildByName("child5").y);
The variable 'Collector' is only available inside the constructor the way you have it. Collector has to be made public to be accessible from outside the timeline. The best thing to do would be to make a public getter method to access this. So something like:
import flash.display.MovieClip;
public class CustomClass extends MovieClip {
private var Collector:Array = new Array();
public function get Collector():Array
{
return Collector;
}
public function CustomClass(amount:uint) {
//Add and position Tiles to stage.
for (var i:uint = 1; i <= amount; i++){
var newMovieClip:MovieClip = new MovieClip;
newMovieClip.y = amount * 10;
Collector.push(newMovieClip);
}
addChild(Collector);
}
}

Actionscript: Am I deleting this class instance correctly?

Okay by deleting correctly I mean am I actually getting rid of the instance or is it just not being drawn anymore? I should mention that I'm trying to delete the instance from within its own class, that is it deletes itself. It 'works' in that the square it draws no longer appears on the screen but again I'm not sure if it's really gone or just not being drawn. Anyway here's the class:
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_bullet_1 extends Sprite
{
public var X:int = 0; public var Y:int = 0;
public var Y_SPEED:int = 5;
public var DEPTH:int = 9;
public var CONTAINER:Sprite = new Sprite();
public function CREATE(CONTAINER:Sprite,X:int,Y:int):void
{
this.CONTAINER = CONTAINER;
CONTAINER.stage.addEventListener(Event.ENTER_FRAME,STEP);
this.X = X; this.Y = Y;
DRAW();
}
public function STEP(event:Event):void
{
this.graphics.clear();
Y -= Y_SPEED;
if (Y < 20) {Y = 300; CONTAINER.removeChild(this); CONTAINER.stage.removeEventListener(Event.ENTER_FRAME,STEP); CONTAINER.(delete this); CONTAINER = null; return;}
DRAW();
}
public function DRAW():void
{
this.graphics.beginFill(0xCCCC00,1);
this.graphics.drawRect(X - 2,Y - 2,4,4);
this.graphics.endFill();
CONTAINER.addChild(this);
}
}
}
The part I'm concerned about is in the STEP function when it checks to see if Y < 20. You'll notice that it does several things afterwords. Am I deleting it correctly? If so is there anything I am doing to delete it that I don't need to?
Yes to both questions. To ensure an object is deleted, all you have to do is remove all references to it. The child reference and event callback are the only ones the above code is aware of, and you have taken care to remove them both. Nullifying your own container reference is unnecessary, as is whatever you think CONTAINER.(delete this) does.
There are some other significant problems with your supplied code. I made some improvements and heavily commented all changes to explain why I made them.
// You should avoid using the default package. Using the default package
// can make it difficult later on if you start having naming conflicts.
package com.stackoverflow.example {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.getTimer;
// Class names are spelled in CamelCase by convention. Also, note
// that "Object" has a special meaning in AS3 so you should avoid
// using it to refer to anything else. I used here "Entity" instead.
public class EntityBullet1 extends Sprite {
// ALLCAPS when used are reserved for static const names.
// A good use of static consts is to store "magic numbers".
public static const DEFAULT_COLOR:uint = 0xCCCC00;
public static const DEFAULT_SPEED_X:Number = 0;
public static const DEFAULT_SPEED_Y:Number = -100;
public static const DEFAULT_SIZE:Number = 4;
// I'm calculating the time between frames for smoother movement.
public var lastTime:int;
public var color:uint = DEFAULT_COLOR;
public var size:int = DEFAULT_SIZE;
// Instead of separate x and y vars, you can use the Point class.
public var pos:Point;
public var speed:Point;
// Instead of a "create" method do all creation inside the constructor!
public function EntityBullet1(x:Number = 0, y:Number = 0) {
pos = new Point(x, y);
speed = new Point(DEFAULT_SPEED_X, DEFAULT_SPEED_Y);
// You don't need the parent container to access the ENTER_FRAME
// event. Every DisplayObject has its own. Much simpler.
addEventListener(Event.ENTER_FRAME, firstStep);
}
public function draw():void {
// Keep all drawing inside the draw function. Previously,
// clear() was being called inside the step method.
graphics.clear();
graphics.beginFill(color);
graphics.drawRect(pos.x - size/2, pos.y - size/2, size, size);
graphics.endFill();
}
// On the first frame, the field "lastTime" is still uninitialized.
// This method initializes it to the current time and hands off
// future events to the proper step() method.
public function firstStep(event:Event):void {
removeEventListener(Event.ENTER_FRAME, firstStep);
addEventListener(Event.ENTER_FRAME, step);
lastTime = getTimer();
step(event);
}
public function step(event:Event):void {
// To move at a fixed rate regardless of how fast the framerate is,
// you need to calculate the time delta.
var cur:int = getTimer();
var delta:Number = (cur - lastTime) / 1000.0;
lastTime = cur;
// Position equals velocity times time.
pos.x += speed.x * delta;
pos.y += speed.y * delta;
draw();
// Note that all DisplayObjects already have references to their
// parent containers called "parent"!
if (pos.y < 20) {
if (parent != null) parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, step);
}
}
}
}