Class in first frame. - actionscript-3

Ok now I am more prepared. I have perfectly fine site. But when compress every thing to one frame. It stop working. Only thing I change is that I remove tween in IDE.
Code in Frame
stop();
import flash.events.MouseEvent;
import fl.transitions.Tween;
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.events.Event;
var vektor:Array = new Array(I,II,III,IV,V,VI,VII,VIII,IX,X,XI,XII);
var menu:Wyjazd = new Wyjazd(vektor,firmaBTN,kontaktBTN,ofertaBTN,naglowek,tekst,dane);
ofertaBTN.addEventListener(MouseEvent.CLICK, test);
function test(e:MouseEvent):void
{
trace(String("click"));
}
Code in class Wyjazd
package
{
import fl.transitions.Tween;
import fl.motion.easing.*;
import flash.filters.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.display.*;
import flash.events.Event;
public class Wyjazd extends MovieClip
{
public function Wyjazd(ar:Array=null,firma:Object=null,kontakt:Object=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null)
{
/*
if (ar!=null)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
*/
if (stage)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(ar:Array,firma:Object=null,kontakt=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null):void
{
//Zmienne "globalne" dla funkcji
var time:Number;
var wciecie:Number;
var wciecie2:Number;
var offset:Number = 15.65;
var offset2:Number = 20;
var posX:Array = new Array(12);
var posY:Array = new Array(12);
var spr:Array = new Array(12);
var targetLabel:String;
var wybranyOb:Object = ar[0];
var names:Array = new Array('Szkolenie wstępne BHP','Szkolenie okresowe BHP','Szkolenie P.Poż','Kompleksowa obsługa P.Poż','Pomiar środowiska pracy','Szkolenie z udzielania pierwszej pomocy','Ocena ryzyka zawodowego','Przeprowadzanie postępowań po wypadkowych','Przeprowadzanie audytów wewnętrznych ISO','Hałas w środowisku komunalnym','Medycyna pracy','Szkolenia dla kierowców');
//Pobieranie pozycji
for (var i:Number = 0; i<ar.length; i++)
{
posX[i] = ar[i].x;
posY[i] = ar[i].y;
trace(String("klasa"));
}
//Filtry
function increaseBlur(e:MouseEvent,docPos:Number):void
{
var myBlur:BlurFilter =new BlurFilter();
myBlur.quality = 3;
myBlur.blurX = 10;
myBlur.blurY = 0;
}
//Funkcje
function startPos():void
{
time = 0.2;
for (var i:Number = 0; i<ar.length; i++)
{
//if (wybranyOb.name == ar[i].name)
//{
//var wybranyPos:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],0.01,true);
//wybranyPos = new Tween(ar[i],"y",Linear.easeOut,-30,posY[i],time,true);
//}
//else
//{
var position:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
position = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
//}
//time = 0.2;
}
position = new Tween(naglowek,"x",Linear.easeOut,naglowek.x,2000,time,true);
position = new Tween(tekst,"x",Linear.easeOut,tekst.x,2000,time,true);
position = new Tween(dane,"x",Linear.easeOut,dane.x,2000,0.25,true);
}
//Nasłuchy
oferta.addEventListener(MouseEvent.CLICK, wyskokOferta);
oferta.addEventListener(MouseEvent.MOUSE_OVER,glowOferta);
oferta.addEventListener(MouseEvent.MOUSE_OUT,unglowOferta);
kontakt.addEventListener(MouseEvent.CLICK,wyskokKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OVER,glowKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OUT,unglowKontakt);
firma.addEventListener(MouseEvent.CLICK,wyskokFirma);
firma.addEventListener(MouseEvent.MOUSE_OVER,glowFirma);
firma.addEventListener(MouseEvent.MOUSE_OUT,unglowFirma);
function glowFirma(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
firma.filters = [myGlow];
}
function unglowFirma(e:MouseEvent):void
{
firma.filters = [];
}
function glowKontakt(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
kontakt.filters = [myGlow];
}
function unglowKontakt(e:MouseEvent):void
{
kontakt.filters = [];
}
function glowOferta(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
oferta.filters = [myGlow];
}
function unglowOferta(e:MouseEvent):void
{
oferta.filters = [];
}
function wyskokKontakt(e:MouseEvent):void
{
startPos();
var tweenKontakt = new Tween(dane,"x",Linear.easeOut,2000,350,0.25,true);
}
function wyskokFirma(e:MouseEvent):void
{
startPos();
trace("Firma");
}
function wyskokOferta(e:MouseEvent):void
{
time = 0.2;
wciecie = 52.5;
wciecie2 = 67.45;
for (var i:Number = 0; i < ar.length; i++)
{
var tween:Tween = new Tween(ar[i],"x",Sine.easeOut,ar[i].x,oferta.x + wciecie,time,true);
tween = new Tween(ar[i],"y",Sine.easeOut,ar[i].y,oferta.y + wciecie2,time,true);
ar[i].addEventListener(MouseEvent.CLICK,onClick);
spr[i] = i;
time += 0.02;
wciecie += offset;
wciecie2 += offset2;
}
}
function onClick(e:MouseEvent)
{
startPos();
time = 0.2;
var k:Number = 0;
targetLabel = e.currentTarget.name;
for (var i:Number = 0; i < ar.length; i++)
{
if (targetLabel==ar[i].name)
{
//wybranyOb = ar[i];
var tween:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
tween = new Tween(naglowek,"x",Linear.easeOut,2000,60,0.2,true);
tween = new Tween(tekst,"x",Linear.easeOut,2000,500,0.25,true);
naglowek.text = names[i];
}
else
{
var tween1:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween1 = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
}
//time += 0.02;
}
}
}
}
}
How it's looks like frame.
http://www.dropmocks.com/mbe3j

You should take all the (nested) functions out of init.
So it is more like this:
package
{
import fl.transitions.Tween;
import fl.motion.easing.*;
import flash.filters.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.display.*;
import flash.events.Event;
public class Wyjazd extends MovieClip
{
public function Wyjazd(ar:Array=null,firma:Object=null,kontakt:Object=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null)
{
/*
if (ar!=null)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
*/
if (stage)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(ar:Array,firma:Object=null,kontakt=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null):void
{
//Zmienne "globalne" dla funkcji
var time:Number;
var wciecie:Number;
var wciecie2:Number;
var offset:Number = 15.65;
var offset2:Number = 20;
var posX:Array = new Array(12);
var posY:Array = new Array(12);
var spr:Array = new Array(12);
var targetLabel:String;
var wybranyOb:Object = ar[0];
var names:Array = new Array('Szkolenie wstępne BHP','Szkolenie okresowe BHP','Szkolenie P.Poż','Kompleksowa obsługa P.Poż','Pomiar środowiska pracy','Szkolenie z udzielania pierwszej pomocy','Ocena ryzyka zawodowego','Przeprowadzanie postępowań po wypadkowych','Przeprowadzanie audytów wewnętrznych ISO','Hałas w środowisku komunalnym','Medycyna pracy','Szkolenia dla kierowców');
//Pobieranie pozycji
for (var i:Number = 0; i<ar.length; i++)
{
posX[i] = ar[i].x;
posY[i] = ar[i].y;
trace(String("klasa"));
}
//Nasłuchy
oferta.addEventListener(MouseEvent.CLICK, wyskokOferta);
oferta.addEventListener(MouseEvent.MOUSE_OVER,glowOferta);
oferta.addEventListener(MouseEvent.MOUSE_OUT,unglowOferta);
kontakt.addEventListener(MouseEvent.CLICK,wyskokKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OVER,glowKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OUT,unglowKontakt);
firma.addEventListener(MouseEvent.CLICK,wyskokFirma);
firma.addEventListener(MouseEvent.MOUSE_OVER,glowFirma);
firma.addEventListener(MouseEvent.MOUSE_OUT,unglowFirma);
}
public function glowFirma(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
firma.filters = [myGlow];
}
public function unglowFirma(e:MouseEvent):void
{
firma.filters = [];
}
public function glowKontakt(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
kontakt.filters = [myGlow];
}
public function unglowKontakt(e:MouseEvent):void
{
kontakt.filters = [];
}
public function glowOferta(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
oferta.filters = [myGlow];
}
public function unglowOferta(e:MouseEvent):void
{
oferta.filters = [];
}
public function wyskokKontakt(e:MouseEvent):void
{
startPos();
var tweenKontakt = new Tween(dane,"x",Linear.easeOut,2000,350,0.25,true);
}
public function wyskokFirma(e:MouseEvent):void
{
startPos();
trace("Firma");
}
public function wyskokOferta(e:MouseEvent):void
{
time = 0.2;
wciecie = 52.5;
wciecie2 = 67.45;
for (var i:Number = 0; i < ar.length; i++)
{
var tween:Tween = new Tween(ar[i],"x",Sine.easeOut,ar[i].x,oferta.x + wciecie,time,true);
tween = new Tween(ar[i], "y", Sine.easeOut, ar[i].y, oferta.y + wciecie2, time, true);
ar[i].addEventListener(MouseEvent.CLICK,onClick);
spr[i] = i;
time += 0.02;
wciecie += offset;
wciecie2 += offset2;
}
}
//Filtry
public function increaseBlur(e:MouseEvent,docPos:Number):void
{
var myBlur:BlurFilter =new BlurFilter();
myBlur.quality = 3;
myBlur.blurX = 10;
myBlur.blurY = 0;
}
//Funkcje
public function startPos():void
{
time = 0.2;
for (var i:Number = 0; i<ar.length; i++)
{
//if (wybranyOb.name == ar[i].name)
//{
//var wybranyPos:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],0.01,true);
//wybranyPos = new Tween(ar[i],"y",Linear.easeOut,-30,posY[i],time,true);
//}
//else
//{
var position:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
position = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
//}
//time = 0.2;
}
position = new Tween(naglowek,"x",Linear.easeOut,naglowek.x,2000,time,true);
position = new Tween(tekst,"x",Linear.easeOut,tekst.x,2000,time,true);
position = new Tween(dane,"x",Linear.easeOut,dane.x,2000,0.25,true);
}
public function onClick(e:MouseEvent)
{
startPos();
time = 0.2;
var k:Number = 0;
targetLabel = e.currentTarget.name;
for (var i:Number = 0; i < ar.length; i++)
{
if (targetLabel==ar[i].name)
{
//wybranyOb = ar[i];
var tween:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
tween = new Tween(naglowek,"x",Linear.easeOut,2000,60,0.2,true);
tween = new Tween(tekst,"x",Linear.easeOut,2000,500,0.25,true);
naglowek.text = names[i];
}
else
{
var tween1:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween1 = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
}
//time += 0.02;
}
}
}
}
If this doesn't help, you should post more details. I don't know how you 'compressed everything' into a single frame, but I suggest you read more about Object Oriented Programming.
A quick Google search resulted in this article, which might help you out with Object Oriented programming in Action Script 3.

Related

how to use .visible properties in as3?

I just want to make some objects visible="false". But I can't. I can make some objects invisible, however it is not working for the objects in the array. Here is my codes. When score=3, objects should be invisible. It is a catching game. I use Adobe Flash CC.
Can u help me?
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.utils.getDefinitionByName;
public class CatchingGame extends MovieClip {
var catcher:Catcher;
var nextObject:Timer;
var objects:Array = new Array();
var score:int = 0;
const speed:Number = 7.0;
public function CatchingGame() {
catcher = new Catcher();
catcher.y = 350;
addChild(catcher);
setNextObject();
addEventListener(Event.ENTER_FRAME, moveObjects);
}
public function setNextObject() {
nextObject = new Timer(1000+Math.random()*1000,1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
nextObject.start();
}
public function newObject(e:Event) {
var goodObjects:Array = ["Circle1","Circle1","Circle1"];
var badObjects:Array = ["Square1","Square2", "Circle2"];
if (Math.random() < .5) {
var r:int = Math.floor(Math.random()*goodObjects.length);
var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
var newObject:MovieClip = new classRef();
newObject.typestr = "good";
} else {
r = Math.floor(Math.random()*badObjects.length);
classRef = getDefinitionByName(badObjects[r]) as Class;
newObject = new classRef();
newObject.typestr = "bad";
}
newObject.x = Math.random()*500;
addChild(newObject);
objects.push(newObject);
setNextObject();
}
public function moveObjects(e:Event) {
for(var i:int=objects.length-1;i>=0;i--) {
objects[i].y += speed;
if (objects[i].y > 400) {
removeChild(objects[i]);
objects.splice(i,1);
}
if (objects[i].hitTestObject(catcher)) {
if (objects[i].typestr == "good") {
score += 1;
}
if (score < 0) score = 0;
scoreDisplay.text = "Score:" +score;
removeChild(objects[i]);
objects.splice(i,1);
}
}
for(var k:int=objects.length-1;k>=0;k--){
if (score == 3){finished.text="Well Done!";
catcher.visible=false;
scoreDisplay.visible=false;
objects[k].visible=false; }
}
catcher.x = mouseX;
}
}
}
Bildiğim kadarıyla şu sekilde olması lazım
this[diziismi(i)].visible = false;
bu şekilde daha önceki projelerimin birinde kullandığımı ve çalıştığını hatırlıyorum

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!

ActionScript 3.0 AddChild Function Not Working

I have tested out my code and the addchild won't work. No errors are outputted.
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.*;
import flash.events.*;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.text.*;
public class Game extends flash.display.MovieClip{
public static const STATE_INIT:int = 10;
public static const STATE_PLAY:int = 20;
public static const STATE_END_GAME:int = 30;
public var gameState:int = 0;
public var score:int = 0;
public var chances:int = 5;
public var bg:MovieClip;
public var enemies:Array;
public var player:MovieClip;
public var level:Number = 0;
public var scoreLabel:TextField = new TextField
public var levelLabel:TextField = new TextField
public var chancesLabel:TextField = new TextField
public var scoreText:TextField = new TextField
public var levelText:TextField = new TextField
public var chancesText:TextField = new TextField
public const SCOREBOARD_Y:Number = 380
public function Game(){
addEventListener(Event.ENTER_FRAME, gameLoop);
bg = new BackImage();
addChild(bg);
scoreLabel.text = "Score:";
levelLabel.text = "level:";
chancesLabel.text = "Misses:";
scoreText.text = "0";
levelText.text = "1";
chancesText.text = "5";
scoreLabel.y = SCOREBOARD_Y;
levelLabel.y = SCOREBOARD_Y;
chancesLabel.y = SCOREBOARD_Y;
scoreText.y = SCOREBOARD_Y;
levelText.y = SCOREBOARD_Y;
chancesText.y = SCOREBOARD_Y;
scoreLabel.x = 5;
scoreText.x = 50;
chancesLabel.x = 105;
chancesText.x = 155;
levelLabel.x = 205;
levelText.x = 260
addChild(scoreLabel);
addChild(levelLabel);
addChild(chancesLabel);
addChild(scoreText);
addChild(levelText);
addChild(chancesText);
gameState = STATE_INIT;
}
public function gameLoop(e:Event):void{
switch(gameState){
case STATE_INIT:
initGame();
break;
case STATE_PLAY:
playGame();
break;
case STATE_END_GAME:
endGame();
break;
}
}
public function initGame():void{
score = 0;
chances = 5;
player = new playerImage();
enemies = new Array();
level = 1;
levelText.text = level.toString();
addChild(player);
player.startDrag(true,new Rectangle(0,0,550,400))
gameState = STATE_PLAY
}
public function playGame():void{
player.rotation += 15;
makeEnemies();
moveEnemies();
testCollisions();
testForEnd();
}
public function makeEnemies():void{
var chance:Number = Math.floor(Math.random()*100);
var tempEnemy:MovieClip;
if (chance < 2 + level) {
tempEnemy = new EnemyImage()
tempEnemy.speed = 3 + level;
tempEnemy.gotoAndStop(Math.floor(Math.random()*5)+1);
tempEnemy.y = 435;
tempEnemy.x = Math.floor(Math.random()*515)
addChild(tempEnemy);
enemies.push(tempEnemy);
}
}
public function moveEnemies():void{
var tempEnemy:MovieClip;
for (var i:int = enemies.length -1;i >= 0;i--){
tempEnemy = enemies[i];
tempEnemy.y -= tempEnemy.speed;
if (tempEnemy.y < -35){
chances -= 1;
chancesText.text = chances.toString();
enemies.splice(i,1);
removeChild(tempEnemy);
}
}
}
public function testCollisions():void {
var sound:Sound = new Pop();
var tempEnemy:MovieClip;
for (var i:int = enemies.length -1;i >= 0;i--){
tempEnemy = enemies[i];
if(tempEnemy.hitTestObject(player)){
score++;
scoreText.text = score.toString();
sound.play();
enemies.splice(i,1);
removeChild(tempEnemy);
}
}
}
public function testForEnd():void{
if(chances == 5){
gameState = STATE_END_GAME;
}
else if(score > level*20) {
level++;
levelText.text = level.toString();
}
}
public function endGame():void{
for(var i:int = 0; i< enemies.length; i++) {
removeChild(enemies[i]);
}
enemies = [];
player.stopDrag()
}
}
}
I have already tried adding this. and stage. in front of addchild and it still doesn't work.
This inside a file called Game.as
When you create your Game instance you need to add it as a child to the stage:
// I would probably rename the variable to 'game'
// 1st character of a variable name is conventionally lower-case
// var game:Game = new Game();
var Script:Game = new Game();
this.addChild(Script);

Action Script 3.0 The public attribute can only be used inside a package

The code below are inside a file called Script_1.as
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.*;
import flash.events.*;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.text.*;
public class Game extends flash.display.MovieClip{
public static const STATE_INIT:int = 10;
public static const STATE_PLAY:int = 20;
public static const STATE_END_GAME:int = 30;
public var gameState:int = 0;
public var score:int = 0;
public var chances:int = 5;
public var bg:MovieClip;
public var enemies:Array;
public var paly:MovieClip;
public var level:Number = 0;
public var scoreLable:TextField = newTextField
public var levelLable:TextField = newTextField
public var chancesLable:TextField = newTextField
public var scoreText:TextField = newTextField
public var levelText:TextField = newTextField
public var chancesText:TextField = newTextField
public const SCOREBOARD_Y:Number = 380
public function Game(){
addEventListener(Event.ENTER_FRAME, gameLoop);
bg = new BackImage();
addChild(bg);
scoreLable.text = "Score:";
levelLable.text = "level:";
chancesLable.text = "Misses:";
scoreText.text = "0";
levelText.text = "1";
chancesText.text = "5";
scoreLable.y = SCOREBOARD_Y;
levelLablel.y = SCOREBOARD_Y;
chancesLabel.y = SCOREBOARD_;
scoreText.y = SCOREBOARD_Y;
levelText.y = SCOREBOARD_Y;
chancesText.y = SCOREBOARD_Y;
scoreLabel.x = 5;
scoreText.x = 50;
chancesLabel.x = 105;
chancesText.x = 155;
levelLabel.x = 205;
levelText.x = 260
addChild(scoreLabel);
addChild(levelLabel);
addChild(chancesLabel);
addChild(scoreText);
addChild(levelText);
addChild(chancesText);
gameState = STATE_INIT;
}
public function gameLoop(e:Event):void{
switch(gameState){
case STATE_INIT:
initGame();
break;
case STATE_PLAY:
playGame();
break;
case STATE_END_GAME:
endGame();
break;
}
//public function initGame():void{
score = 0;
chances = 5;
player = new playerImage();
enemies = newArray();
level = 1;
levelText.text = leveltoString();
addChild(player);
player.startDrag(true,newRectangle(0,0,550,400))
gameState = STATE_PLAY
}
//public function playGame():void{
player.rotation += 15;
makeEnemies();
moveEnemies();
testCollisions();
testForEnd();
}
//public function makeEnemies():void{
var chance:Number = Math.floor(Math.random()*100);
var tempEnemy:MovieClip;
if (chance < 2 + level) {
tempEnemy = new EnemyImage()
tempEnemy.speed = 3 + level;
tempEnemy.gotoAndStop(Math.floor(Math.randome()*5)+1);
tempEnemy.y = 435;
tempEnemy.x = Math.floor(Math.randome()*515)
addChild(tempEnemy);
enemies.push(tempEnemy);
}
}
//public function moveEnemies():void{
/var tempEnemy:MovieClip;
for (var i:int = enemies.length -1;i >= 0;i--){
tempEnemy = enemies[i];
tempEnemy.y -= tempEnemy.speed;
if (tempEnemy.y < -35){
chances -= 1;
chancesText.text = chances.toString();
enemies.splice(i,1);
removeChild(tempEnemy);
}
}
}
public function testCollisions();void {
var sound:Sound = new Pop();
var tempEnemy:MovieClip;
for (var i:int = enemies.length -1;i >= 0;i--){
tempEnemy = enemies[i];
if(tempEnemy.hitTestObject(player)){
score++;
scoreText.text = score.toString();
sound.play();
enemies.splice(i,1);
removeChild(tempEnemy);
}
}
}
public function testForEnd():void{
if(chances == 5){
gameState = STATE_END_GAME;
}else if(score > level*20) {
level++;
levelText.text = level.toString();
}
}
public function endGame():void{
for(var i:int = 0; i< enemies.length; i++) {
removeChild(enemies[i]);
}
enemies = [];
player.stopDrag()
}
}
}
The Error Reports are:
The Public Attribute Can Only Be Used In A Package
(I put // before these Error Lines to mark them. They are not in the original code.)
Syntax error: expecting identifier before var.
Syntax error: expecting rightbrace before semicolon.
(I put / before these Errors. they are on the same line and the / is not in the original code either.)
Try rename your file to Game.as.
try to add a } after your switch case to close the function gameLoop().
PS:
Is it really your code ? Math.randome() ? newTextField ? newArray() ? public function testCollisions();void ? (;void)

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