AS3 Flash CS6 access of undefined properties? Help will be much obliged - actionscript-3

Properties such as score1; score2, lives1, lives2 and STAGE seem to be undefined properties. I dont see why? Please help...
e.g.
C:\Users\PC\Desktop\Whack My Mole - Android\Game.as, Line 429 1120: Access of undefined property score1.
C:\Users\PC\Desktop\Whack My Mole - Android\Game.as, Line 430 1120: Access of undefined property score1.
package {
import flash.display.Stage;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.DropShadowFilter;
import flash.text.AntiAliasType;
import flash.text.Font;
public class Game{
//Global Declarations
private var STAGE:Stage;
'Graphics'
private var Title:Title_mc;
private var Score_lbl:Label_Score_txt;
private var Lives_lbl:Label_Lives_txt;
private var holes:MoleHoles_mc;
private var Lives_txt:TextField;
private var Score_txt:TextField;
private var Shadow:DropShadowFilter;
private var Pause_btn:Button_Pause;
'Game Properties'
private var SLEEP:Timer;
private var countdown:Timer;
private var countdownComplete:Timer;
private var countdownDelay:Timer;
private var count:TextField;
private var count_inc:int;
private var Paused:Boolean;
private var moles:int;
public static var mole_spawn_delay:int = 1000;
public static var mole_death_delay:int = 1000;
public static var molePlacable:Boolean = true;
private var unavailableHole:int;
public static var molesName:String;
private var lvl:int = 51;
private var score1:Anim_Score1_mc;
private var score2:Anim_Score2_mc;
private var lives1:Anim_Lives1_mc;
private var lives2:Anim_Lives2_mc;
'Player Properties'
public static var Score:int = 0;
public static var Lives:int = 3;
public static var incrementer:int = 1;
public function Game(STAGE:Stage) {
this.STAGE = STAGE;
//Enable Touch Events
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//Instantiate Objects
Shadow = new DropShadowFilter();
//Handle new frames
STAGE.addEventListener(Event.ENTER_FRAME, handle_ENTER_FRAME);
//Draw Graphics
'Menu Title'
Title = new Title_mc();
Title.x = 250.5;
Title.y = 62.35;
STAGE.addChild(Title);
'Content'
holes = new MoleHoles_mc();
holes.x = 0;
holes.y = 123.9;
STAGE.addChild(holes);
'Score Label'
Score_lbl = new Label_Score_txt();
Score_lbl.x = 19.65;
Score_lbl.y = 146.75;
STAGE.addChild(Score_lbl);
'Lives Label'
Lives_lbl = new Label_Lives_txt();
Lives_lbl.x = 358.25;
Lives_lbl.y = 141.05;
STAGE.addChild(Lives_lbl);
'Score Value'
Score_txt = new TextField();
Score_txt.x = 19.65;
Score_txt.y = 209.95;
Score_txt.width = 100;
Score_txt.defaultTextFormat = new TextFormat("Balloonist", 40, 0xFFFFFF);
Score_txt.selectable = false;
Score_txt.text = String(Score);
STAGE.addChild(Score_txt);
'Lives Value'
Lives_txt = new TextField();
Lives_txt.x = 410;
Lives_txt.y = 204.70;
Lives_txt.defaultTextFormat = new TextFormat("Balloonist", 40, 0xFFFFFF);
Lives_txt.selectable = false;
Lives_txt.text = String(Lives);
STAGE.addChild(Lives_txt);
'Pause Button'
Pause_btn = new Button_Pause();
Pause_btn.x = 22.40;
Pause_btn.y = 772.85;
STAGE.addChild(Pause_btn);
Pause_btn.buttonMode = true;
Pause_btn.addEventListener(TouchEvent.TOUCH_BEGIN, handle_Pause);
//Start Countdown
countDown();
}
//Pause/resume game
private function handle_Pause(e:TouchEvent):void
{
//...
}
//Sleep Method
private function sleep(seconds:int):void
{
SLEEP = new Timer(seconds, 1); // 1 second
SLEEP.addEventListener(TimerEvent.TIMER_COMPLETE, sleep_end);
SLEEP.start();
STAGE.frameRate = 0;
}
//Sleep Method Complete
private function sleep_end(e:TimerEvent):void
{
SLEEP.removeEventListener(TimerEvent.TIMER_COMPLETE , sleep_end);
STAGE.frameRate = 24;
}
//Count Down Timer
private function countDown():void
{
Paused = true;
count_inc = 5;
count = new TextField();
count.x = 213.9;
count.y = 158.05;
count.height = 150;
count.width = 150;
count.defaultTextFormat = new TextFormat("Balloonist", 150, 0xFFFFFF);
count.filters = [Shadow];
count.antiAliasType = AntiAliasType.ADVANCED;
count.sharpness = 400;
count.text = String(count_inc);
STAGE.addChild(count);
countdownComplete = new Timer(5000, 1);
countdownComplete.addEventListener(TimerEvent.TIMER, coutdown_Complete);
countdownDelay = new Timer(100);
countdownDelay.addEventListener(TimerEvent.TIMER, countDown_Tick);
countdown = new Timer(1000);
countdown.addEventListener(TimerEvent.TIMER, countDown_end);
countdownComplete.start();
countdownDelay.start();
countdown.start();
}
//Handle countdown tick
private function countDown_Tick(e:TimerEvent):void
{
if(count_inc <= 0)
{
countdown.stop();
countdown.removeEventListener(TimerEvent.TIMER, countDown_end);
}else {
countdownDelay.delay = 100;
}
}
//Handle countown complete
private function countDown_end(e:TimerEvent):void
{
if(count_inc <= 0)
{
countdownDelay.stop();
countdownDelay.removeEventListener(TimerEvent.TIMER, countDown_Tick);
}else{
count_inc -= 1;
count.text = String(count_inc);
}
}
//Countdown cleanup
private function coutdown_Complete(e:TimerEvent):void
{
STAGE.removeChild(count);
Paused = false;
}
//Main Game Loop
private function handle_ENTER_FRAME(e:Event):void
{
//Update game stuff
if(!Paused)
{
if(molePlacable)
{
sleep(mole_spawn_delay);
newMole();
}
Score_txt.text = String(Score);
Lives_txt.text = String(Lives);
}
//Clear stage & display game over interface
if(Lives <= 0)
{
STAGE.removeEventListener(Event.ENTER_FRAME, handle_ENTER_FRAME);
STAGE.removeChild(Title);
STAGE.removeChild(holes);
STAGE.removeChild(Score_lbl);
STAGE.removeChild(Lives_lbl);
STAGE.removeChild(Pause_btn);
STAGE.removeChild(Score_txt);
STAGE.removeChild(Lives_txt);
var gameOver:GameOver = new GameOver(STAGE);
}
//Update mole stats
if(moles > 50)
{
lvl = 71;
}
//Dissallow score to go below 0
if(Score < 0)
{
Score = 0;
}
}
//Create new Mole
private function newMole():void
{
'Specify mole hole'
var rnd = Math.ceil(Math.random()*11);
'Ensure mole does not spawn from preceding hole'
while(rnd == unavailableHole)
{
rnd = Math.ceil(Math.random()*11);
}
var X:int;
var Y:int;
switch(rnd)
{
case 0:
X = -14.75;
Y = 293.45;
break;
case 1:
X = 109.25;
Y = 291.35;
break;
case 2:
X = 223.75;
Y = 291.35;
break;
case 3:
X = 337.2;
Y = 291.35;
break;
case 4:
X = 0;
Y = 430;
break;
case 5:
X = 118.7;
Y = 430;
break;
case 6:
X = 226.9;
Y = 430;
break;
case 7:
X = 342.45;
Y = 430
break;
case 8:
X = 0;
Y = 561.35
break;
case 9:
X = 112.4;
Y = 561.35;
break;
case 10:
X = 229;
Y = 561.35;
break;
case 11:
X = 339.3;
Y = 561.35;
break;
}
'Specify molde to add'
rnd = lvl * Math.random();
if(rnd <=40)
{
//Default + 10*incrementer
var mole:Mole_Default_mc = new Mole_Default_mc(STAGE, X, Y);
}else if(rnd <=42){
//Crazy - 5*incrementer
var mole2:Mole_Crazy_mc = new Mole_Crazy_mc(STAGE, X, Y);
}else if(rnd <=43){
//Crazy2 - 10*inrementer
var mole3:Mole_Crazy2_mc = new Mole_Crazy2_mc(STAGE, X, Y);
}else if(rnd <45){
//Lady + 1 life
var mole4:Mole_Lady_mc= new Mole_Lady_mc(STAGE, X, Y);
}else if(rnd <46){
//Ninja - 2*inrementer
var mole5:Mole_Ninja_mc = new Mole_Ninja_mc(STAGE, X, Y);
}else if(rnd <47){
//Zombie + 5 * lives
var mole6:Mole_Zombie_mc = new Mole_Zombie_mc(STAGE, X, Y);
}else if(rnd <48){
//reaper - Lives
var mole7:Mole_Reaper_mc = new Mole_Reaper_mc(STAGE, X, Y);
}else if(rnd <49){
//Snob + 250
var mole8:Mole_Snob_mc = new Mole_Snob_mc(STAGE, X, Y);
}else if(rnd <52){
//Angel + 3 lives
var mole9:Mole_Angel_mc = new Mole_Angel_mc(STAGE, X, Y);
}else if(rnd <54){
//Demon - 3 lives
var mole10:Mole_Demon_mc = new Mole_Demon_mc(STAGE, X, Y);
}else if(rnd <55){
//Creature - 3+incrementer
var creature:Mole_Creature_mc = new Mole_Creature_mc(STAGE, X, Y);
}else if(rnd <56){
//Cyber + 50+incrementer
var cyber:Mole_Cyber_mc = new Mole_Cyber_mc(STAGE, X, Y);
}else if(rnd <57){
//Grumpy + 5
var grumpy:Mole_Grumpy_mc = new Mole_Grumpy_mc(STAGE, X, Y);
}else if(rnd <58){
//Hippie Lives+3 Score+100
var hippie:Mole_Hippie_mc = new Mole_Hippie_mc(STAGE, X, Y);
}else if(rnd<59){
//Hyper 30*incrementer
var hyper:Mole_Hyper_mc = new Mole_Hyper_mc(STAGE, X, Y);
}else if(rnd<60){
//Love timer-100
var love:Mole_Love_mc = new Mole_Love_mc(STAGE, X, Y);
}else if(rnd<61){
//LoveZombie - 20*Lives
var loveZombie:Mole_LoveZombie_mc = new Mole_LoveZombie_mc(STAGE, X, Y);
}else if(rnd<70){
//Sleepy Timer+100
var sleepy:Mole_Sleepy_mc = new Mole_Sleepy_mc(STAGE, X, Y);
}else if(rnd<71){
//Warrior + (10*incrementer)*2
var warrior:Mole_Warrior_mc = new Mole_Warrior_mc(STAGE, X, Y);
}
//Update mole stats
moles += 1;
if(mole_spawn_delay > 20 && mole_death_delay > 20)
{
mole_spawn_delay -= 10;
mole_death_delay -= 5;
}
//Update incrementer
if(moles > 100)
{
incrementer = 50;
}else if(moles > 80)
{
incrementer = 40;
}else if(moles > 60)
{
incrementer = 30;
}else if(moles > 20)
{
incrementer = 20;
}else if(moles > 10)
{
incrementer = 10;
}
}
//Animation
public static function animate(type:int)
{
if(type == 0)
{
score1 = new Anim_Score1_mc();
score1.x = 40;
score1.y = 250.6;
STAGE.addChild(score1);
var anim_timer:Timer = new Timer(2000, 1);
anim_timer.addEventListener(TimerEvent.TIMER, remove_score1);
anim_timer.start();
}else if(type == 1)
{
score2 = new Anim_Score2_mc();
score2.x = 32;
score2.y = 248.6;
STAGE.addChild(score2);
var anim_timer2:Timer = new Timer(2000, 1);
anim_timer2.addEventListener(TimerEvent.TIMER, remove_score2);
anim_timer2.start();
}else if(type == 2)
{
lives1 = new Anim_Lives1_mc();
lives1.x = 430.9;
lives1.y = 237.95;
STAGE.addChild(lives1);
var anim_timer3:Timer = new Timer(2000, 1);
anim_timer3.addEventListener(TimerEvent.TIMER, remove_lives1);
anim_timer3.start();
}else{
lives2 = new Anim_Lives2_mc();
lives2.x = 430.9;
lives2.y = 237.95;
STAGE.addChild(lives2);
var anim_timer4:Timer = new Timer(2000, 1);
anim_timer4.addEventListener(TimerEvent.TIMER, remove_lives1);
anim_timer4.start();
}
}
//Handle remove_score1
private function remove_score1(e:TimerEvent):void
{
STAGE.removeChild(score1);
}
//Handle remove_score2
private function remove_score2(e:TimerEvent):void
{
STAGE.removeChild(score2);
}
//Handle remove_lives1
private function remove_lives1(e:TimerEvent):void
{
STAGE.removeChild(lives1);
}
//Handle remove_lives2
private function remove_lives2(e:TimerEvent):void
{
STAGE.removeChild(lives2);
}
}
}

Replace:
public static function animate(type:int)
with
public function animate(type:int)
Or if you want to have that method static add:
private static var _instance:Game = null;
in Game Class definition and change animate function:
public static function animate(type:int):void{
if(_instance!=null) _instance.hidden_animate(type);
}
protected function hidden_animate(type:int){
// PLACE HERE CODE FROM YOUR ORIGINAL ANIMATE FUNCTION
}
in constructor function add:
_instance = this;
If you often use static methods (like in this code) read more bout design patterns like singleton :)

Related

State Machine Cant get to work

Learning to code so this might sound noobish...
Using Action Script
I am trying to make my Agents react to my segments so they run away.
So I need to connect part of my Main code to my Agent code
Here is my code :
Main.as
package
{
import agent.Agent;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.display.Stage;
public class Main extends Sprite
{
private var score:Score;
private var count:int;
private var bleh: int = 0;
private var Agents:Vector.<Agent>;
private var segments:Array;
private var numSegments:uint = 20;
private var player:Point = new Point (200, 200)
private var friend:Agent = new Agent;
private var snakeHead:SnakeHead;
private var snakeTail:SnakeTail;
private var background: Sprite;
private var border: Bushes;
private var xVel: Number;
private var yVel: Number;
public function Main():void
{
xVel = 0;
yVel = 0;
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
snakeHead = new SnakeHead();
snakeTail = new SnakeTail();
score = new Score();
border = new Bushes;
score.x = 11;
score.y = 14;
addChild(score);
stage.addChild(border);
count = 0;
addChildAt(snakeHead,0);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
segments = new Array();
for(var i:uint = 0; i < numSegments; i++)
{
var segment:Segment = new Segment (10, 20);
addChildAt(segment,0);
addChildAt(snakeTail,0);
segments.push(segment);
}
//updatePoint();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// entry point
background = new LevelOne(); //creates the background
addChildAt(background,0);
Agents = new Vector.<Agent>();
addEventListener(Event.ENTER_FRAME, gameloop);
for (var x:int = 0; x < 3; x++)
{
var a:Agent = new Agent();
addChild(a);
Agents.push(a);
a.x = 400;
a.y = 300;
a.name = "Name"+x;
trace (x);
}
stage.addEventListener(MouseEvent.CLICK, createAgent);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
private function createAgent(e:MouseEvent):void
{
var a:Agent = new Agent();
addChild(a);
Agents.push(a);
a.x = mouseX;
a.y = mouseY;
}
private function gameloop(evt:Event):void
{
for (var i:int = 0; i < Agents.length; i++)
{
Agents[i].update();
if (snakeHead.hitTestObject(Agents[i]))
{
//trace (Agents[i].name);
removeAgent(Agents[i]);
Agents.splice(i,1);
count ++;
score.lowercasetext.text = count.toString();
trace("count: " + count);
}
}
}
private function onEnterFrame(evt:Event):void
{
player.x += xVel;
player.y += yVel;
drag(snakeHead, player.x, player.y);
drag(segments[0], snakeHead.x, snakeHead.y);
for(var i:uint = 1; i < numSegments; i++)
{
var segmentA:Segment = segments[i];
var segmentB:Segment = segments[i - 1];
drag(segmentA, segmentB.x, segmentB.y);
var PlayerHalfWidth: Number = segments[i].width / 2;
var PlayerHalfHeight: Number = segments[i].height / 2;
if (segments[i].x + PlayerHalfWidth > stage.stageWidth) {
segments[i].x = stage.stageWidth - PlayerHalfWidth;
} else if (segments[i].x - PlayerHalfWidth < 0) {
segments[i].x = 0 + PlayerHalfWidth;
}
if (segments[i].y + PlayerHalfHeight > stage.stageHeight) {
segments[i].y = stage.stageHeight - PlayerHalfHeight;
}else if (segments[i].y - PlayerHalfHeight < 0) {
segments[i].y = 0 + PlayerHalfHeight;
}
var playerHalfWidth: Number = segments[i - 1].width / 2;
var playerHalfHeight: Number = segments[i - 1].height / 2;
if (segments[i - 1].x + playerHalfWidth > stage.stageWidth) {
segments[i - 1].x = stage.stageWidth - playerHalfWidth;
} else if (segments[i - 1].x - playerHalfWidth < 0) {
segments[i - 1].x = 0 + playerHalfWidth;
}
if (segments[i - 1].y + playerHalfHeight > stage.stageHeight) {
segments[i - 1].y = stage.stageHeight - playerHalfHeight;
}else if (segments[i - 1].y - playerHalfHeight < 0) {
segments[i - 1].y = 0 + playerHalfHeight;
}
}
drag(snakeTail, segments[19].x, segments[19].y);
var HeadHalfWidth: Number = snakeHead.width / 2;
var HeadHalfHeight: Number = snakeHead.height / 2;
if (snakeHead.x + HeadHalfWidth > stage.stageWidth) {
snakeHead.x = stage.stageWidth - HeadHalfWidth;
} else if (snakeHead.x - HeadHalfWidth < 0) {
snakeHead.x = 0 + HeadHalfWidth;
}
if (snakeHead.y + HeadHalfHeight > stage.stageHeight) {
snakeHead.y = stage.stageHeight - HeadHalfHeight;
}else if (snakeHead.y - HeadHalfHeight < 0) {
snakeHead.y = 0 + HeadHalfHeight;
}
//drag(segments[19], snakeTail.x, snakeTail.y);
/*for each (var a: Agent in Agents) {
a.update();
trace ("Follow me on Twitter.");
if(segments[0].hitTestObject(a))
{
trace("True");
trace(bleh + " " + "F*CKING HELL!");
trace(a.name);
stage.removeChild(Agents[1]);
}
}*/
}
private function removeAgent(a:Agent):void
{
a.parent.removeChild(a);
trace("Collision Detected!");
}
private function keyDown (evt: KeyboardEvent): void {
//87=w 68=d 83=s 65=a
if (evt.keyCode == 87)
{
player;
yVel = -5;
}
if (evt.keyCode == 83)
{
player;
yVel = 5;
}
if (evt.keyCode == 68)
{
player;
xVel = 5;
}
if (evt.keyCode == 65)
{
player;
xVel = -5;
}
//trace (player.x + " " + player.y);
trace (xVel + " " + yVel);
}
private function keyUp (evt: KeyboardEvent): void {
//87=w 68=d 83=s 65=a
if (evt.keyCode == 87)
{
player;
yVel = 0;
}
else if (evt.keyCode == 83)
{
player;
yVel = 0;
}
else if (evt.keyCode == 68)
{
player;
xVel = 0;
}
else if (evt.keyCode == 65)
{
player;
xVel = 0;
}
}
private function drag(segment:MovieClip, xpos:Number, ypos:Number):void
{
var dx:Number = xpos - segment.x;
var dy:Number = ypos - segment.y;
var angle:Number = Math.atan2(dy, dx);
segment.rotation = angle * 180 / Math.PI;
var w:Number = segment.getPin().x - segment.x;
var h:Number = segment.getPin().y - segment.y;
segment.x = xpos - w;
segment.y = ypos - h;
}
}
}
Agent.as
package agent
{
import agent.states.ChaseState;
import agent.states.ConfusionState;
import agent.states.FleeState;
import agent.states.IAgentState;
import agent.states.IdleState;
import agent.states.WanderState;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.display.MovieClip;
import flash.events.*;
import Segment;
import Main;
public class Agent extends MovieClip
{
private var mouses:mouse;
public static const IDLE:IAgentState = new IdleState(); //Define possible states as static constants
public static const WANDER:IAgentState = new WanderState();
public static const CHASE:IAgentState = new ChaseState();
public static const FLEE:IAgentState = new FleeState();
public static const CONFUSED:IAgentState = new ConfusionState();
public var snake:Segment;
private const RAD_DEG:Number = 180 / Math.PI;
private var _previousState:IAgentState; //The previous executing state
private var _currentState:IAgentState; //The currently executing state
private var _pointer:Shape;
private var _tf:TextField;
public var velocity:Point = new Point();
public var speed:Number = 0;
public var fleeRadius:Number = 100; //If the mouse is "seen" within this radius, we want to flee
public var chaseRadius:Number = 50; //If the mouse is "seen" within this radius, we want to chase
public var numCycles:int = 0; //Number of updates that have executed for the current state. Timing utility.
public function Agent()
{
//Boring stuff here
_tf = new TextField();
_tf.defaultTextFormat = new TextFormat("_sans", 10);
_tf.autoSize = TextFieldAutoSize.LEFT;
_pointer = new Shape();
mouses = new mouse();
snake = new Segment(1, 1);
/*g.beginFill(0);
g.drawCircle(0, 0, 5);
g.endFill();
g.moveTo(0, -5);
g.beginFill(0);
g.lineTo(10, 0);
g.lineTo(0, 5);
g.endFill();*/
addChild(mouses);
addChild(_tf);
_currentState = IDLE; //Set the initial state
}
/**
* Outputs a line of text above the agent's head
* #param str
*/
public function say(str:String):void {
_tf.text = str;
_tf.y = -_tf.height - 2;
}
/**
* Trig utility methods
*/
public function get canSeeMouse():Boolean {
var dot:Number = snake.x * velocity.x + snake.y * velocity.y;
return dot > 0;
}
public function get distanceToMouse():Number {
var dx:Number = x - snake.x;
var dy:Number = y - snake.y;
return Math.sqrt(dx * dx + dy * dy);
}
public function randomDirection():void {
var a:Number = Math.random() * 6.28;
velocity.x = Math.cos(a);
velocity.y = Math.sin(a);
}
public function faceMouse(multiplier:Number = 1):void {
var dx:Number = snake.x - x;
var dy:Number = snake.y - y;
var rad:Number = Math.atan2(dy, dx);
velocity.x = multiplier*Math.cos(rad);
velocity.y = multiplier*Math.sin(rad);
}
/**
* Update the current state, then update the graphics
*/
public function update():void {
if (!_currentState) return; //If there's no behavior, we do nothing
numCycles++;
_currentState.update(this);
x += velocity.x*speed;
y += velocity.y*speed;
if (x + velocity.x > stage.stageWidth || x + velocity.x < 0) {
x = Math.max(0, Math.min(stage.stageWidth, x));
velocity.x *= -1;
}
if (y + velocity.y > stage.stageHeight || y + velocity.y < 0) {
y = Math.max(0, Math.min(stage.stageHeight, y));
velocity.y *= -1;
}
mouses.rotation = RAD_DEG * Math.atan2(velocity.y, velocity.x);
}
public function setState(newState:IAgentState):void {
if (_currentState == newState) return;
if (_currentState) {
_currentState.exit(this);
}
_previousState = _currentState;
_currentState = newState;
_currentState.enter(this);
numCycles = 0;
}
public function get previousState():IAgentState { return _previousState; }
public function get currentState():IAgentState { return _currentState; }
}
}

How do I remove eventlisteners and objects from an array upon game completion?

I have written a drag and drop game and, for the most part, it works.
It runs and does what I need it to do.
However, I cannot figure out two things.
How to remove the toy objects from the screen and re-add them after game over.
How to remove the event listeners for dragging/collision action after game over has initiated.
At the moment, after score is displayed you can still drop items in the toybox and make the score rise even when game over has been displayed.
Does anyone have any idea what I am doing wrong?
I would love to figure this out but it is driving me crazy.
Any help would be great.
Here is my code ...
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.Font;
import flash.filters.GlowFilter;
public class MainGame extends MovieClip {
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var tryAgain:TryAgain = new TryAgain;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000, 30);
var myText:TextField = new TextField;
var myText2:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var myTextFormat2:TextFormat = new TextFormat;
var font1:Font1 = new Font1;
var kids:Kids = new Kids;
var count:int = 0;
var finalScore:int = 0;
var score:Number = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
public function mainGame():void
{
trace("HI");
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChildAt(kids, 2);
kids.x = 310;
kids.y = 200;
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
}
public function bgScroll (e:Event)
{
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
e.target.addEventListener(Event.ENTER_FRAME, collision);
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var m:int=0; m< toyArray.length; m++)
{
(toyArray[m] as MovieClip).x -=BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int=0; j< toyArray.length; j++)
{
(toyArray[j] as MovieClip).x +=BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
} // End of BGScroll
public function collision (e:Event)
{
for (var l:int=0; l< toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x=100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
if (score == 200)
{
timerDone();
myTimer.stop();
}
}
}
public function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
public function arrayDrop(e:MouseEvent)
{
stopDrag();
}
public function resetGame(e:Event):void {
trace("CLICK");
countDown = 30;
myText.text = "0" + countDown.toString();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.reset();
removeChild(tryAgain);
myText2.visible = false;
score = 0;
}
public function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
if (countDown < 10)
{
myText.text = "0" + countDown.toString();
myText.x = 270;
displayText();
}
else if (countDown < 20 && countDown > 9)
{
myText.text = countDown.toString();
myText.x = 280;
displayText();
}
else
{
myText.text = countDown.toString();
myText.x = 270;
displayText();
}
} // end of countdown function
public function displayText():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 50, myText.y = 10;
myTextFormat.size = 50, myTextFormat.font = font1.fontName;
myText.setTextFormat(myTextFormat);
}
public function displayText2():void
{ myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
addChild(myText2);
myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;
myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;
myText2.setTextFormat(myTextFormat2);
}
public function timerDone(e:TimerEvent=null):void
{
if (countDown == 0)
{
count = 0;
finalScore = score;
}
else
{
count = (30) - (myTimer.currentCount);
finalScore = (count * 10) + (score);
}
myText.text = "GAME OVER!";
myText.x = 195;
displayText();
myText2.text = "Your score = " + (finalScore);
displayText2();
addChild(tryAgain);
tryAgain.x = 300;
tryAgain.y = 300;
tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
}
} // End of class
} //End of package
Nevermind ... solved it.
Easiest was was to change
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
To
function addToys(xpos:int, ypos:int)
{
stage.addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
And then, in the timerDone function I added thisif statement ...
for (var m = 0; m < toyArray.length; m++) {
if (stage.contains(toyArray[m])) {
stage.removeChild(toyArray[m]);
}
Worked a treat!

User interaction with Leapmotion AS3 library

I can connect the device and attach a custom cursor to one finger, but I can´t use any of the gestures to over/click a button or drag a sprite around, etc.
I´m using Starling in the project. To run this sample just create a Main.as, setup it with Starling and call this class.
My basic code:
package
{
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.events.LeapEvent;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.InteractionBox;
import com.leapmotion.leap.Pointable;
import com.leapmotion.leap.ScreenTapGesture;
import com.leapmotion.leap.Vector3;
import starling.display.Shape;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.TouchEvent;
/**
* ...
* #author miau
*/
public class LeapController extends Sprite
{
private var _controller:Controller;
private var _cursor:Shape;
private var _screenTap:ScreenTapGesture;
private var _displayWidth:uint = 800;
private var _displayHeight:uint = 600;
public function LeapController()
{
addEventListener(Event.ADDED_TO_STAGE, _startController);
}
private function _startController(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, _startController);
//adding controller
_controller = new Controller();
_controller.addEventListener( LeapEvent.LEAPMOTION_INIT, onInit );
_controller.addEventListener( LeapEvent.LEAPMOTION_CONNECTED, onConnect );
_controller.addEventListener( LeapEvent.LEAPMOTION_DISCONNECTED, onDisconnect );
_controller.addEventListener( LeapEvent.LEAPMOTION_EXIT, onExit );
_controller.addEventListener( LeapEvent.LEAPMOTION_FRAME, onFrame );
//add test button
_testButton.x = stage.stageWidth / 2 - _testButton.width / 2;
_testButton.y = stage.stageHeight / 2 - _testButton.height / 2;
addChild(_testButton);
_testButton.touchable = true;
_testButton.addEventListener(TouchEvent.TOUCH, doSomething);
//draw ellipse as a cursor
_cursor = new Shape();
_cursor.graphics.lineStyle(6, 0xFFE24F);
_cursor.graphics.drawEllipse(0, 0, 80, 80);
addChild(_cursor);
}
private function onFrame(e:LeapEvent):void
{
trace("ON FRAME STARTED");
var frame:Frame = e.frame;
var interactionBox:InteractionBox = frame.interactionBox;
// Get the first hand
if(frame.hands.length > 0){
var hand:Hand = frame.hands[0];
var numpointables:int = e.frame.pointables.length;
var pointablesArray:Array = new Array();
if(frame.pointables.length > 0 && frame.pointables.length < 2){
//trace("number of pointables: "+frame.pointables[0]);
for(var j:int = 0; j < frame.pointables.length; j++){
//var pointer:DisplayObject = pointablesArray[j];
if(j < numpointables){
var pointable:Pointable = frame.pointables[j];
var normal:Vector3 = pointable.tipPosition;
var normalized:Vector3 = interactionBox.normalizePoint(normal);
//pointable.isFinger = true;
_cursor.x = normalized.x * _displayWidth;
_cursor.y = _displayHeight - (normalized.y * _displayHeight);
_cursor.visible = true;
}else if (j == 0) {
_cursor.visible = false;
}
}
}
}
}
private function onExit(e:LeapEvent):void
{
trace("ON EXIT STARTED");
}
private function onDisconnect(e:LeapEvent):void
{
trace("ON DISCONNECT STARTED");
}
private function onConnect(e:LeapEvent):void
{
trace("ON CONNECT STARTED");
_controller.enableGesture( Gesture.TYPE_SWIPE );
_controller.enableGesture( Gesture.TYPE_CIRCLE );
_controller.enableGesture( Gesture.TYPE_SCREEN_TAP );
_controller.enableGesture( Gesture.TYPE_KEY_TAP );
}
private function onInit(e:LeapEvent):void
{
trace("ON INIT STARTED");
}
private function doSomething(e:TouchEvent):void
{
trace("I WAS TOUCHED!!!");
}
}
}
If a good code Samaritan can update this code to perform a screen tap gesture (or any interacion with any object), I will really appreciate this a lot.
Regards!
controller.enableGesture(Gesture.TYPE_SWIPE);
controller.enableGesture(Gesture.TYPE_SCREEN_TAP);
if(controller.config().setFloat("Gesture.Swipe.MinLength", 200.0) && controller.config().setFloat("Gesture.Swipe.MinVelocity", 500)) controller.config().save();
if(controller.config().setFloat("Gesture.ScreenTap.MinForwardVelocity", 30.0) && controller.config().setFloat("Gesture.ScreenTap.HistorySeconds", .5) && controller.config().setFloat("Gesture.ScreenTap.MinDistance", 1.0)) controller.config().save();
//etc...
Then catch it in the frame event listener:
private function onFrame( event:LeapEvent ):void
{
var frame:Frame = event.frame;
var gestures:Vector.<Gesture> = frame.gestures();
for ( var i:int = 0; i < gestures.length; i++ )
{
var gesture:Gesture = gestures[ i ];
switch ( gesture.type )
{
case Gesture.TYPE_SCREEN_TAP:
var screentap:ScreenTapGesture = ScreenTapGesture ( gesture);
trace ("ScreenTapGesture-> x: " + Math.round(screentap.position.x ) + ", y: "+ Math.round( screentap.position.y));
break;
case Gesture.TYPE_SWIPE:
var screenSwipe:SwipeGesture = SwipeGesture(gesture);
if(gesture.state == Gesture.STATE_START) {
//
}
else if(gesture.state == Gesture.STATE_STOP) {
//
trace("SwipeGesture-> direction: "+screenSwipe.direction + ", duration: " + screenSwipe.duration);
}
break;
default:
trace( "Unknown gesture type." )
}
}
}
When the event occurs, check the coordinates translated to the stage/screen and whether a hit test returns true.
EDIT: Considering I have no idea how to reliable get the touch point x/y (or better: how to translate them to the correct screen coordinates), I would probably do something like this in my onFrame event:
private function onFrame(event:LeapEvent):void {
var frame:Frame = event.frame;
var gestures:Vector.<Gesture> = frame.gestures();
var posX:Number;
var posY:Number;
var s:Shape;
if(frame.pointables.length > 0) {
var currentVector:Vector3 = screen.intersectPointable(frame.pointables[0], true); //get normalized vector
posX = 1920 * currentVector.x - stage.x; //NOTE: I hardcoded the screen res value, you can get it like var w:int = leap.locatedScreens()[0].widthPixels();
posY = 1080 * ( 1 - currentVector.y ) - stage.y; //NOTE: I hardcoded the screen res value, you can get it like var h:int = leap.locatedScreens()[0].heightPixels();
}
for(var i:int = 0; i < gestures.length; i++) {
var gesture:Gesture = gestures[i];
if(gesture.type == Gesture.TYPE_SCREEN_TAP) {
if(posX >= _button1.x &&
posX <= _button1.x + _button1.width &&
posY >= _button1.y &&
posY <= _button1.y + _button1.height) {
s = new Shape();
s.graphics.beginFill(0x00FF00);
s.graphics.drawCircle(0, 0, 10);
s.graphics.endFill();
s.x = posX;
s.y = posY;
stage.addChild(s);
trace("Lisa tocada!");
}
else {
s = new Shape();
s.graphics.beginFill(0xFF0000);
s.graphics.drawCircle(0, 0, 10);
s.graphics.endFill();
s.x = posX;
s.y = posY;
stage.addChild(s);
trace("Fallaste! Intentalo otra vez, tiempo: "+new Date().getTime());
}
}
}
}

Netstream audio playing twice

I am new to flash (this is the first time I've ever used it or actionscript) and I'm trying to make a video player. The video player gets params from the embed code and pulls the videos from a folder on the server.
I've got the following code (I've removed everything that I'm 100% sure isn't causing my problem):
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.NetStatusEvent;
import flash.events.MouseEvent;
import flash.events.FullScreenEvent;
import flash.events.Event;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextFormat;
import flash.media.SoundTransform;
var nc:NetConnection;
var ns:NetStream;
var ns2:NetStream;
var video:Video;
var video2:Video;
//get filename parameters from embed code
var filename:String = root.loaderInfo.parameters.filename;
var filename2:String = root.loaderInfo.parameters.filename2;
var t:Timer = new Timer(5000);
var duration;
var currentPosition:Number;
var st:Number;
var started:Boolean;
Object(this).mcPlay.buttonMode = true;
Object(this).mcPause.buttonMode = true;
Object(this).ScreenClick.buttonMode = true;
Object(this).mcMax.buttonMode = true;
Object(this).mcSwitcher.buttonMode = true;
Object(this).mcPause.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).mcPlay.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).ScreenClick.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).mcMax.addEventListener(MouseEvent.CLICK,Maximize);
Object(this).slideVolume.addEventListener(Event.CHANGE, ChangeVolume);
Object(this).mcSwitcher.addEventListener(MouseEvent.CLICK, ToggleSwitcher);
t.addEventListener(TimerEvent.TIMER, TimerComplete);
stage.addEventListener(MouseEvent.MOUSE_MOVE, resetTimer);
stage.addEventListener(MouseEvent.MOUSE_DOWN, resetTimer);
stage.addEventListener(MouseEvent.MOUSE_UP, resetTimer);
stage.addEventListener(Event.ENTER_FRAME, videoTimer);
if (!nc) start();
var IsPaused:String;
function start():void
{
Object(this).slideVolume.maximum = 100;
Object(this).slideVolume.value = 100;
started = false;
var tf:TextFormat = new TextFormat();
tf.color = 0xFFFFFF;
tf.bold = true;
this.lblTime.setStyle("textFormat", tf);
connect();
t.start();
}
function connect():void
{
nc = new NetConnection();
nc.client = this;
nc.addEventListener(NetStatusEvent.NET_STATUS, OnNetStatus);
nc.connect(null);
}
function OnNetStatus(e:NetStatusEvent):void
{
switch(e.info.code)
{
case "NetConnection.Connect.Success":
if (!started)
{
started = true;
stream();
}
else
{
finish();
}
break;
default:
finish();
break;
}
}
function stream():void
{
ns = new NetStream(nc);
ns.client = this;
ns.bufferTime = 5; // set the buffer time to 5 seconds
if ((filename2 != null) && (filename2.length > 0))
{
ns2 = new NetStream(nc)
//ns2.client = this; //Uncomment to use ns2 vid for duration info
ns2.bufferTime = 5; // set the buffer time to 5 seconds
startVideo(2);
currentPosition = 1; //Default
ns.seek(0);
ns2.seek(0);
}
else
{
this.mcSwitcher.visible = false;
startVideo(1);
ns.seek(0);
}
}
function startVideo(num:Number):void
{
var startVolume:SoundTransform = new SoundTransform();
startVolume.volume = slideVolume.value / 100;
if (num == 2)
{
video = new Video(320,180);
video.x = 0;
video.y = 90;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play(filename); //path/filename
setChildIndex(video,1);
video2 = new Video(320,180);
video2.x = 320;
video2.y = 90;
addChild(video2);
video2.attachNetStream(ns2);
ns2.checkPolicyFile = false;
ns2.play(filename2); //path/filename
setChildIndex(video2,1);
ns.soundTransform = startVolume;
var videoVolumeTransform2:SoundTransform = new SoundTransform();
videoVolumeTransform2.volume = 0;
ns2.soundTransform = videoVolumeTransform2;
ns2.receiveAudio(false);
}
else if (num == 1)
{
video = new Video(640,360);
video.x = 0;
video.y = 0;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play(filename); //path/filename
setChildIndex(video,1);
ns.soundTransform = startVolume;
}
IsPaused = "playing";
this.removeChild(mcPlay);
setChildIndex(this.ScreenClick,0);
setChildIndex(this.mcTitleOverlay,2);
}
function ShowControls ():void
{
for (var i:int = 0; i < Object(root).numChildren; i++)
{
switch (Object(root).getChildAt(i))
{
case mcPause:
if (IsPaused != "paused")
Object(root).getChildAt(i).visible = true;
break;
case mcPlay:
if (IsPaused != "playing")
Object(root).getChildAt(i).visible = true;
break;
case mcSwitcher:
if ((filename2 != null) && (filename2.length > 0))
Object(root).getChildAt(i).visible = true;
break;
default:
Object(root).getChildAt(i).visible = true; //Bring back everything else
break;
}
ScreenClick.y = 154;
}
}
function videoTimer(e:Event):void
{
var curTime = ns.time; //Current time in seconds
var curMinutes = Math.floor(curTime / 60); //Get the minutes
var curSeconds = Math.floor(curTime % 60); //Get the leftover seconds
var durMinutes = Math.floor(duration / 60);
var durSeconds = Math.floor(duration % 60);
//Add the zeroes to the begining of the seconds if it is needed.
if (curSeconds < 10)
curSeconds = "0" + curSeconds;
if (durSeconds < 10)
durSeconds = "0" + durSeconds;
Object(this).lblTime.text = curMinutes + ":" + curSeconds + " / " + durMinutes + ":" + durSeconds;
}
function PlayPause (e:MouseEvent):void
{
switch (IsPaused)
{
case "playing":
IsPaused = "paused";
this.mcPlay.visible = true;
this.mcPause.visible = false;
ns.togglePause();
ns2.togglePause();
break;
case "paused":
IsPaused = "playing";
this.mcPause.visible = true;
this.mcPlay.visible = false;
ns.togglePause();
ns2.togglePause();
break;
default:
//
break;
}
}
The problem I have is small but frustrating (I've spend most of today trying to figure it out with zero progress made). It is thus: Everything works perfectly, except that when the videos load up and play, sound plays twice (for the video that has sound enabled). I am at my wits end trying to figure this out, any help would be greatly appreciated!
Thanks!
EDIT:
Ok, on further research (re-writing every function very simply and seeing if the problem goes away with the features) I've determined that the following function is the root of all evil (or at least my problems):
function startVideo(num:Number):void
{
var startVolume:SoundTransform = new SoundTransform();
startVolume.volume = Object(this).slideVolume.sldVol.value / 100;
if (num == 2)
{
video = new Video(320,180);
video.x = 0;
video.y = 90;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play(filename); //path/filename
this.removeChild(btnPlay);
setChildIndex(video,1);
video2 = new Video(320,180);
video2.x = 320;
video2.y = 90;
addChild(video2);
video2.attachNetStream(ns2);
ns2.checkPolicyFile = false;
ns2.play("test.mp4"); //path/filename
setChildIndex(video2,1);
ns.soundTransform = startVolume;
var videoVolumeTransform2:SoundTransform = new SoundTransform();
videoVolumeTransform2.volume = 0;
ns2.soundTransform = videoVolumeTransform2;
ns2.receiveAudio(false);
}
else if (num == 1)
{
video = new Video(640,360);
video.x = 0;
video.y = 0;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play("test.flv"); //path/filename
setChildIndex(video,1);
ns.soundTransform = startVolume;
}
IsPaused = "playing";
this.removeChild(btnPlay);
setChildIndex(this.ScreenClick,0);
//setChildIndex(this.mcTitleOverlay,2);
}
I shall persevere with my troubleshooting (I've isolated the problem, hopefully the next step is a solution!

doubt regarding carrying data in custom events using actionscript

I am working on actionscript to generate a SWF dynamically using JSON data coming from an HTTP request. I receive the data on creationComplete and try to generate a tree like structure. I don’t create the whole tree at the same time. I create 2 levels, level 1 and level 2. My goal is to attach custom events on the panels which represent tree nodes. When users click the panels, it dispatches custom events and try to generate the next level. So, it goes like this :
On creation complete -> get JSON-> create top tow levels -> click on level 2-> create the level 2 and level 3 -> click on level 3-> create level 3 and 4. …and so on and so on. I am attaching my code with this email. Please take a look at it and if you have any hints on how you would do this if you need to paint a tree having total level number = “n” where n = 0 to 100
Should I carry the data around in CustomPageClickEvent class.
[code]
import com.iwobanas.effects.*;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.filters.GradientGlowFilter;
import mx.controls.Alert;
private var roundedMask:Sprite;
private var panel:NewPanel;
public var oldPanelIds:Array = new Array();
public var pages:Array = new Array();
public var delPages:Array = new Array();
public function DrawPlaybook(pos:Number,title:String,chld:Object):void {
panel = new NewPanel(chld);
panel.title = title;
panel.name=title;
panel.width = 100;
panel.height = 80;
panel.x=pos+5;
panel.y=40;
var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
gradientGlow.distance = 0;
gradientGlow.angle = 45;
gradientGlow.colors = [0xFFFFF0, 0xFFFFFF];
gradientGlow.alphas = [0, 1];
gradientGlow.ratios = [0, 255];
gradientGlow.blurX = 10;
gradientGlow.blurY = 10;
gradientGlow.strength = 2;
gradientGlow.quality = BitmapFilterQuality.HIGH;
gradientGlow.type = BitmapFilterType.OUTER;
panel.filters =[gradientGlow];
this.rawChildren.addChild(panel);
pages.push(panel);
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onClickHandler(e,title,chld)});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
}
public function onClickHandler(e:MouseEvent,title:String,chld:Object):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPanelClicked(e:CustomPageClickEvent,title:String):void {
Alert.show("onCustomPanelClicked" + title);
var panel:NewPanel;
for each(var stp:NewPanel in pages){
startAnimation(e,stp);
}
if(title == e.panelClicked.title){
panel = new NewPanel(null);
panel.title = title;
panel.name=title;
panel.width = 150;
panel.height = 80;
panel.x=100;
panel.y=40;
this.rawChildren.addChild(panel);
var slideRight:SlideRight = new SlideRight();
slideRight.target=panel;
slideRight.duration=750;
slideRight.showTarget=true;
slideRight.play();
var jsonData = this.map.getValue(title);
var posX:Number = 50;
var posY:Number = 175;
for each ( var pnl:NewPanel in pages){
pages.pop();
}
for each ( var stp1:Object in jsonData.children){
panel = new NewPanel(null);
panel.title = stp1.text;
panel.name=stp1.id;
panel.width = 100;
panel.id=stp1.id;
panel.height = 80;
panel.x = posX;
panel.y=posY;
posX+=150;
var s:String="hi" + stp1.text;
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onChildClick(e,s);});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPnlClicked(e)});
this.rawChildren.addChild(panel);
pages.push(panel);
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
var slide:SlideUp = new SlideUp();
slide.target=panel;
slide.duration=1500;
slide.showTarget=false;
slide.play();
}
}
}
public function onChildClick(e:MouseEvent,s:String):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==e.currentTarget.title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPnlClicked(e:CustomPageClickEvent):void {
for each ( var pnl:NewPanel in pages){
pages.pop();
}
}
private function fadePanel(event:Event,panel:NewPanel):void{
panel.alpha -= .005;
if (panel.alpha <= 0){
panel.removeEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel);});
};
panel.title="";
}
private function startAnimation(event:CustomPageClickEvent,panel:NewPanel):void{
panel.addEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel)});
}
[/code]
Thanks in advance.
Palash
completely forgot i don't have enough rep to edit...
import com.iwobanas.effects.*;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.filters.GradientGlowFilter;
import mx.controls.Alert;
private var roundedMask:Sprite;
private var panel:NewPanel;
public var oldPanelIds:Array = new Array();
public var pages:Array = new Array();
public var delPages:Array = new Array();
public function DrawPlaybook(pos:Number,title:String,chld:Object):void {
panel = new NewPanel(chld);
panel.title = title;
panel.name=title;
panel.width = 100;
panel.height = 80;
panel.x=pos+5;
panel.y=40;
var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
gradientGlow.distance = 0;
gradientGlow.angle = 45;
gradientGlow.colors = [0xFFFFF0, 0xFFFFFF];
gradientGlow.alphas = [0, 1];
gradientGlow.ratios = [0, 255];
gradientGlow.blurX = 10;
gradientGlow.blurY = 10;
gradientGlow.strength = 2;
gradientGlow.quality = BitmapFilterQuality.HIGH;
gradientGlow.type = BitmapFilterType.OUTER;
panel.filters = [gradientGlow];
this.rawChildren.addChild(panel);
pages.push(panel);
panel.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){onClickHandler(e,title,chld)});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
}
public function onClickHandler(e:MouseEvent,title:String,chld:Object):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPanelClicked(e:CustomPageClickEvent,title:String):void {
Alert.show("onCustomPanelClicked" + title);
var panel:NewPanel;
for each(var stp:NewPanel in pages){
startAnimation(e,stp);
}
if(title == e.panelClicked.title){
panel = new NewPanel(null);
panel.title = title;
panel.name=title;
panel.width = 150;
panel.height = 80;
panel.x=100;
panel.y=40;
this.rawChildren.addChild(panel);
var slideRight:SlideRight = new SlideRight();
slideRight.target=panel;
slideRight.duration=750;
slideRight.showTarget=true;
slideRight.play();
var jsonData = this.map.getValue(title);
var posX:Number = 50;
var posY:Number = 175;
for each ( var pnl:NewPanel in pages){
pages.pop();
}
for each ( var stp1:Object in jsonData.children){
panel = new NewPanel(null);
panel.title = stp1.text;
panel.name=stp1.id;
panel.width = 100;
panel.id=stp1.id;
panel.height = 80;
panel.x = posX;
panel.y=posY;
posX += 150;
var s:String="hi" + stp1.text;
panel.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){onChildClick(e,s);});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPnlClicked(e)});
this.rawChildren.addChild(panel);
pages.push(panel);
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
var slide:SlideUp = new SlideUp();
slide.target=panel;
slide.duration=1500;
slide.showTarget=false;
slide.play();
}
}
}
public function onChildClick(e:MouseEvent,s:String):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==e.currentTarget.title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPnlClicked(e:CustomPageClickEvent):void {
for each ( var pnl:NewPanel in pages){
pages.pop();
}
}
private function fadePanel(event:Event,panel:NewPanel):void{
panel.alpha -= .005;
if (panel.alpha <= 0){
panel.removeEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel);});
};
panel.title="";
}
private function startAnimation(event:CustomPageClickEvent,panel:NewPanel):void{
panel.addEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel)});
}