I honestly can't see what I am missing! Everything adds up however it seems my public class GameBackground is not closing properly, please help! I must have been staring at this for so long I can't see what is right in front of me!
package com./////./////.objects
{
import starling.display.Sprite;
import starling.events.Event;
public class GameBackground extends Sprite
(
private var bgLayer1:BgLayer;
private var bgLayer2:BgLayer;
private var bgLayer3:BgLayer;
private var bgLayer4:BgLayer;
private var _speed:Number = 0;
public function GameBackground()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
bgLayer1 = new BgLayer(1);
bgLayer1.parallax = 0.02;
this.addChild(bgLayer1);
bgLayer2 = new BgLayer(2);
bgLayer2.parallax = 0.2;
this.addChild(bgLayer2);
bgLayer3 = new BgLayer(3);
bgLayer3.parallax = 0.5;
this.addChild(bgLayer3);
bgLayer4 = new BgLayer(4);
bgLayer4.parallax = 1;
this.addChild(bgLayer4);
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
bgLayer1.x -= Math.ceil(_speed * bgLayer1.parallax);
if(bgLayer1 < -stage.stageWidth)
{
bgLayer1.x = 0;
}
bgLayer2.x -= Math.ceil(_speed * bgLayer2.parallax);
if(bgLayer2 < -stage.stageWidth)
{
bgLayer2.x = 0;
}
bgLayer3.x -= Math.ceil(_speed * bgLayer3.parallax);
if(bgLayer3 < -stage.stageWidth)
{
bgLayer3.x = 0;
}
bgLayer4.x -= Math.ceil(_speed * bgLayer4.parallax);
if(bgLayer4 < -stage.stageWidth)
{
bgLayer4.x = 0;
}
}
public function get speed():Number
{
return _speed;
}
public function set speed(value:Number):void
{
_speed = value;
}
}
}
Under your class declaration you have a parentheses instead of a bracket. Enabled "permit debugging" to get line numbers and other useful information
I'm trying to do do a BattleShip Flash game using ActionScript 3.
1- I draw a grid (class: com.Battleship.grid) using a gridCell:movieClip (class: com.battleship.GridCell)
2- I draw a ship (class: com.battleship.Bateau)
My objective is when I drag the ship to the grid the alpha cell under the mouse changes, and if I drop:
1- if the droptarget is a gridCell, the ship takes place (works fine)
2- if the droptarget is stage, the ship return to originPosition (not working)
My problem: after I used MouseEnabled= false, the ship can drop on the grid, but not on the stage and still dragging.
Here is my code:
var grid:Grid = new Grid(10,new Point(20,20));
var b1:Bateau = new Bateau();
b1.x = 490;
b1.y = 300;
b1.originPos = new Point(b1.x,b1.y);
addChild(b1)
addChild(grid);
package com.battleship {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.events.Event;
public class Bateau extends MovieClip {
private var _originPos:Point;
public function Bateau() {
originPos = new Point();
buttonMode = true;
addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
}
protected function onDrag(e:MouseEvent):void
{
e.currentTarget.parent.addChild(e.currentTarget);
e.currentTarget.startDrag();
e.currentTarget.mouseEnabled = false;
//e.currentTarget.mouseChildren = false;
addEventListener(MouseEvent.MOUSE_UP, onDrop);
parent.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
protected function onDrop(e:MouseEvent):void
{
e.currentTarget.mouseEnabled = true;
e.currentTarget.mouseChildren = true;
e.currentTarget.stopDrag();
if(dropTarget == null){
x = originPos.x;
y = originPos.y;
}else{
trace(e.currentTarget.name);
if(dropTarget.parent is GridCell)
{
x = dropTarget.parent.x+20;
y = dropTarget.parent.y;
}
}
}
public function get originPos():Point
{
return _originPos;
}
public function set originPos(originPos:Point):void
{
_originPos = originPos;
}
}
}
package com.battleship {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class GridCell extends MovieClip {
private var _row:int;
private var _col:int;
private var _val:int;
public function GridCell()
{
addEventListener(MouseEvent.CLICK,onClick);
addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
}
public function onClick(e:MouseEvent)
{
//trace("(" + e.currentTarget.row+ ", " + e.currentTarget.col + ")");
trace(e.currentTarget.name);
}
public function onMouseOver(e:MouseEvent)
{
alpha = 0.6;
}
public function onMouseOut(e:MouseEvent)
{
alpha = 1;
}
public function get row():int
{
return _row;
}
public function set row(row:int):void
{
_row = row;
}
public function get col():int
{
return _col;
}
public function set col(col:int):void
{
_col = col;
}
public function get val():int
{
return _val;
}
public function set val(val:int):void
{
_val = val;
}
}
}
package com.battleship {
import flash.geom.Point;
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Grid extends Sprite {
private var _size:int;
private var _position:Point;
public function Grid(size:int,position:Point):void
{
_size = size;
_position = position;
var gridCells:Array = new Array(size,size);
var i,j:int;
for(i=0;i<size;i++)
{
gridCells[i] = new Array();
for(j=0;j<size;j++)
{
var cell:GridCell = new GridCell();
cell.row = j+1;
cell.col = i+1;
cell.x = this.position.x + cell.width * (i+1);
cell.y = this.position.x + cell.height * (j+1);
cell.name = "cell" + (j+1) + (i+1);
//cell.addEventListener(MouseEvent.CLICK,onClick);
gridCells[i][j] = cell;
this.addChild(gridCells[i][j]);
}
}
}
public function get size():int
{
return _size;
}
public function set size(size:int):void
{
_size = size;
}
public function get position():Point
{
return _position;
}
public function set position(position:Point):void
{
_position = position;
}
}
}
So basically this is a question about why my calling class (Door) can not identify this static variable class (Game State). it is throwing a #1009 error saying I can not access with GameState.gameState. On my stage I have a Symbol (CampaignLevel_001) that contains additional symbols like the calling symbol (Door) and above that as a separate Symbol (GameState) so essentially I am having trouble getting a nested symbol / class talking to another class.
package game.GameStates
{
import game.Assets.Player;
import game.Assets.Turret;
import game.Assets.Door;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Point;
import game.Levels.CampaignLevel_001;
public class GameState extends MovieClip
{
//VARIABLES
public var enemyArray : Array;
public var Bullets : Array = new Array();
public var Keys : Array = new Array();
public var HeldKeys : Array = new Array ();
public var Door : Array = new Array();
public var Terrain : Array = new Array();
public var Turrets : Array = new Array ();
public var Blood : Array = new Array ();
public var Shields : Array = new Array ();
public var Levels : Array = new Array ();
public var NumKeys : int;
public var DoorLock : Boolean = false;
public var PlayerSpawnPoint : Point;
public var CampaignSpawnPoint : Point;
public static var gameState : GameState;
public var player : Player;
public var turret : Turret;
public var PlayerLives : int = 99;
public function GameState()
{ // constructor code
gameState = this;
addEventListener(Event.ADDED_TO_STAGE, Awake);
} // constructor code
private function Awake (e : Event) : void
{
enemyArray = new Array();
trace(enemyArray.length);
}
public function OpenDoor () : void
{
if (NumKeys <= 0)
{
DoorLock = true;
}
if (NumKeys > 0)
{
DoorLock = false;
}
}
public function NextLevel () : void
{
RemoveListeners();
trace("SHOULD BE GOING TO THE NEXT LEVEL");
while (Levels.length > 0)
{
for each (var level in Levels)
{
level.NextLevel ();
}
}
}
public function RespawnPlayer () : void
{
//Spawn Player at player start point
player = new Player();
player.x = PlayerSpawnPoint.x;
player.y = PlayerSpawnPoint.y;
addChild(player);
trace("PLAYER ARRAY IS THIS LONG " + enemyArray.length);
trace("spawned Player!");
}
public function setSpellIcons (spell : String , opacity : int) : void
{
if (spell == "dodge")
{
if (opacity == 0)
{
dodgeSymbol.alpha = 0;
}
if (opacity == 1)
{
dodgeSymbol.alpha = 1;
}
}
if (spell == "shield")
{
if (opacity == 0)
{
shieldSymbol.alpha = 0;
}
if (opacity == 1)
{
shieldSymbol.alpha = 1;
}
}
} //public function setSpellIcons (spell : String , opacity : int) : void
public function RemoveListeners() : void
{
while (enemyArray.length > 0)
{
for each (var enemy : MovieClip in enemyArray)
{
enemy.RemovePlayer ();
}
}
while (Door.length > 0)
{
for each (var door : MovieClip in Door)
{
door.RemoveDoorFunctions ();
}
}
while (Bullets.length > 0)
{
for each (var bullet : MovieClip in Bullets)
{
bullet.RemoveProjectile();
}
}
while (Keys.length > 0)
{
for each (var key : MovieClip in Keys)
{
key.RemoveKey ();
}
}
while (Terrain.length > 0)
{
for each (var terrain : MovieClip in Terrain)
{
terrain.RemoveTerrainListeners();
}
}
while (Turrets.length > 0)
{
for each (var turret in Turrets)
{
turret.RemoveTurretListeners();
}
}
while (Blood.length > 0)
{
for each (var splatter in Blood)
{
splatter.RemoveBlood();
}
}
while (Shields.length > 0)
{
for each (var shield in Shields)
{
shield.RemoveShield();
}
}
} //public function RemoveListeners() : void
}
}
The Class that holds (door)
package game.Levels
{
import game.GameStates.GameState;
import flash.display.MovieClip;
import flash.events.Event;
public class CampaignLevel_001 extends MovieClip
{
var currentLevel : int = currentFrame;
public function CampaignLevel_001()
{ // constructor code
addEventListener(Event.ADDED_TO_STAGE, Awake);
}
private function Awake (e : Event) : void
{
GameState.gameState.Levels.push(this);
gotoAndStop(currentLevel);
trace ("CURRENT LEVEL IS " + currentLevel);
}
public function NextLevel () : void
{
var nextLevel : int = currentFrame + 1;
gotoAndStop(nextLevel);
trace ("NEXT LEVEL IS " + nextLevel);
}
}
}
and the calling class (door) is
package game.Assets
{
import flash.display.MovieClip;
import flash.events.Event;
import game.GameStates.GameState;
public class Door extends MovieClip
{
public function Door()
{ // constructor code
addEventListener(Event.ADDED_TO_STAGE, Awake);
}
private function Awake (e : Event) : void
{
GameState.gameState.Door.push(this);
GameState.gameState.OpenDoor ();
stage.addEventListener(Event.ENTER_FRAME, update);
open.alpha = 0;
}
private function update (e : Event) : void
{
if (GameState.gameState.DoorLock == true)
{
open.alpha = 1;
}
if (GameState.gameState.DoorLock == false)
{
open.alpha = 0;
}
}
public function RemoveDoorFunctions () : void
{
GameState.gameState.Door.splice(GameState.gameState.Door.indexOf(this),1);
stage.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
}
}
}
It looks like you're trying to make a Singleton, but haven't added the instance function.
change this:
public static var gameState:GameState;
to:
private static var _gameState:GameState;
and remove the constructor gamestate = this;
and add a function:
public static function get gameState():GameState {
if{_gameState == null) {
_gameState = new GameState();
}
return _gameState;
}
You can then call it and only get the one instance of GameState by:
GameState.gameState.DoorLock == true;
and any other function in GameState the same way
I am currently trying to get acclimated to FlashDevelop, but am having trouble moving an entity, despite following tutorials very closely.
public class Main extends Engine
{
private var gameWorld:MyWorld;
public function Main()
{
super(800, 600, 60, false);
gameWorld = new MyWorld;
}
override public function init():void
{
FP.world = gameWorld;
trace("FlashPunk has started successfully!");
}
}
public class MyWorld extends World
{
private var gameEntity:MyEntity;
public function MyWorld()
{
gameEntity = new MyEntity();
add(gameEntity);
}
override public function update():void
{
//trace("MyEntity updates");
}
}
public class MyEntity extends Entity
{
[Embed(source = "dragon.png")] private const PLAYER:Class;
public function MyEntity()
{
name = "player";
graphic = new Image(PLAYER);
}
override public function update():void
{
x += 10;
y += 5;
//if (Input.check(Key.LEFT)) { x -= 5; }
//if (Input.check(Key.RIGHT)) { x += 5; }
//if (Input.check(Key.UP)) { y -= 5; }
//if (Input.check(Key.DOWN)) { y += 5; }
}
override public function added():void
{
trace("Entity added");
}
}
It is supposed to move across the screen, but does not for some reason. Any thoughts?
So, I got this .as file that is called, let say, class A.
class A inside has other 2 classes, class B and class C, and the only class that is inside a package is class A. And it throws that error.
I downloaded this as an example, and it should work, however Flash Builder 4.6 doesn't like it.
The structure of the as file is like this:
imports
variables
class B
class C
package
public class A
/package
Btw, I'm using Flash Builder not Flash CC.
Update, posting code:
import com.adobe.serialization.json.JSON;
import com.shephertz.appwarp.WarpClient;
import com.shephertz.appwarp.listener.ConnectionRequestListener;
import com.shephertz.appwarp.listener.NotificationListener;
import com.shephertz.appwarp.listener.RoomRequestListener;
import com.shephertz.appwarp.listener.ZoneRequestListener;
import com.shephertz.appwarp.messages.Chat;
import com.shephertz.appwarp.messages.LiveResult;
import com.shephertz.appwarp.messages.LiveRoom;
import com.shephertz.appwarp.messages.LiveUser;
import com.shephertz.appwarp.messages.Lobby;
import com.shephertz.appwarp.messages.MatchedRooms;
import com.shephertz.appwarp.messages.Move;
import com.shephertz.appwarp.messages.Room;
import com.shephertz.appwarp.types.ResultCode;
import flash.utils.ByteArray;
var APIKEY:String = "key";
var SECRETEKEY:String = "secretkey";
var Connected:Boolean = false;
var INITIALIZED:Boolean = false;
var client:WarpClient;
var roomID:String;
var State:int = 0;
var User:String;
class connectionListener implements ConnectionRequestListener
{
private var connectFunc:Function;
public function connectionListener(f:Function)
{
connectFunc = f;
}
public function onConnectDone(res:int):void
{
if(res == ResultCode.success)
{
Connected = true;
}
else
{
Connected = false;
}
connectFunc(res);
}
public function onDisConnectDone(res:int):void
{
Connected = false;
}
}
class roomListener implements RoomRequestListener
{
private var connectFunc:Function;
private var joinFunc:Function;
public function roomListener(f:Function,f1:Function)
{
connectFunc = f;
joinFunc = f1;
}
public function onSubscribeRoomDone(event:Room):void
{
if(State == 2)
joinFunc();
else
connectFunc();
}
public function onUnsubscribeRoomDone(event:Room):void
{
}
public function onJoinRoomDone(event:Room):void
{
if(event.result == ResultCode.resource_not_found)
{
if(State == 1)
{
State = 3;
}
client.createRoom("room","admin",2,null);
}
else if(event.result == ResultCode.success)
{
if(State == 1)
{
State = 2;
}
roomID = event.roomId;
client.subscribeRoom(roomID);
}
}
public function onLeaveRoomDone(event:Room):void
{
client.unsubscribeRoom(roomID);
}
public function onGetLiveRoomInfoDone(event:LiveRoom):void
{
}
public function onSetCustomRoomDataDone(event:LiveRoom):void
{
}
public function onUpdatePropertyDone(event:LiveRoom):void
{
}
public function onLockPropertiesDone(result:int):void
{
}
public function onUnlockPropertiesDone(result:int):void
{
}
public function onUpdatePropertiesDone(event:LiveRoom):void
{
}
}
class zoneListener implements ZoneRequestListener
{
public function onCreateRoomDone(event:Room):void
{
roomID = event.roomId;
client.joinRoom(roomID);
}
public function onDeleteRoomDone(event:Room):void
{
}
public function onGetLiveUserInfoDone(event:LiveUser):void
{
}
public function onGetAllRoomsDone(event:LiveResult):void
{
}
public function onGetOnlineUsersDone(event:LiveResult):void
{
}
public function onSetCustomUserInfoDone(event:LiveUser):void
{
}
public function onGetMatchedRoomsDone(event:MatchedRooms):void
{
}
}
class notifylistener implements NotificationListener
{
private var joinFunc:Function;
private var msgFunc:Function;
private var leaveFunc:Function;
public function notifylistener(f:Function)
{
joinFunc = f;
}
public function msgListener(f:Function,f1:Function):void
{
msgFunc = f;
leaveFunc = f1;
}
public function onRoomCreated(event:Room):void
{
}
public function onRoomDestroyed(event:Room):void
{
}
public function onUserLeftRoom(event:Room, user:String):void
{
if(user != User)
{
leaveFunc();
}
}
public function onUserJoinedRoom(event:Room, user:String):void
{
if(State == 3)
joinFunc();
}
public function onUserLeftLobby(event:Lobby, user:String):void
{
}
public function onUserJoinedLobby(event:Lobby, user:String):void
{
}
public function onChatReceived(event:Chat):void
{
if(event.sender != User)
{
var obj:Object = com.adobe.serialization.json.JSON.decode(event.chat);
msgFunc(obj);
}
}
public function onUpdatePeersReceived(update:ByteArray):void
{
}
public function onUserChangeRoomProperty(room:Room, user:String,properties:Object):void
{
}
public function onPrivateChatReceived(sender:String, chat:String):void
{
}
public function onUserChangeRoomProperties(room:Room, user:String,properties:Object, lockTable:Object):void
{
}
public function onMoveCompleted(move:Move):void
{
}
}
package
{
import com.adobe.serialization.json.JSON;
import com.shephertz.appwarp.WarpClient;
public class AppWarp
{
public static var _roomlistener:roomListener;
public static var _zonelistener:zoneListener;
public static var _notifylistener:notifylistener;
public static var _connectionlistener:connectionListener;
private static function generateRandomString(strlen:Number):String{
var chars:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var num_chars:Number = chars.length - 1;
var randomChar:String = "";
for (var i:Number = 0; i < strlen; i++){
randomChar += chars.charAt(Math.floor(Math.random() * num_chars));
}
return randomChar;
}
public static function connect(f:Function):void
{
if(INITIALIZED == false)
{
WarpClient.initialize(APIKEY, SECRETEKEY);
client = WarpClient.getInstance();
INITIALIZED = true;
}
if(Connected == false)
{
_connectionlistener = new connectionListener(f);
client.setConnectionRequestListener(_connectionlistener);
User = generateRandomString(16);
client.connect(User);
}
else
f(0);
}
public static function join(f1:Function, f2:Function):void
{
_roomlistener = new roomListener(f1,f2);
_zonelistener = new zoneListener();
_notifylistener = new notifylistener(f2);
client.setRoomRequestListener(_roomlistener);
client.setZoneRequestListener(_zonelistener);
client.setNotificationListener(_notifylistener);
State = 1;
client.joinRoomInRange(1,1,true);
}
public static function leave():void
{
client.leaveRoom(roomID);
}
public static function begin(f:Function, f1:Function, dir:int, x:int, y:int):void
{
_notifylistener.msgListener(f, f1);
send(0,dir,x,y);
}
public static function move(dir:int,x:int,y:int):void
{
send(1,dir,x,y);
}
public static function eat(dir:int,x:int,y:int):void
{
send(2,dir,x,y);
}
public static function send(type:int,dir:int,x:int,y:int):void
{
if(Connected == true)
{
var obj:Object = new Object();
obj.type = type;
obj.dir = dir;
obj.x = x;
obj.y = y;
client.sendChat(com.adobe.serialization.json.JSON.encode(obj));
}
}
}
}
I needed to place the package keyword at the very beginning of the .as file, otherwise an error is thrown.
You can only define one class in a package in a file. You can define other classes outside the package, but I don't think that is a good idea as I have observed that in some versions of compiler you have to place it at the beginning and in some at the end. Sometimes, it won't work in anyway.
A better way is to define different classes for each listener in different files. You can use the same package name.
I would recommend creating a single class to listen to all listeners by implementing all base listener classes.
For e.g.
//listener.as
package
{
public class Listener implements ConnectionRequestListener, RoomRequestListener, NotificationListener
{
}
}