Stage properties in custom class, not Document Class - actionscript-3

I need to use stage.width/height in my CustomClass so I found some topics about it.
if (stage)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
But in my case it won't work because it isn't document class, I think. Any other solution?
UPDATE:
CLASS CODE
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.*;
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 (stage)
{
//The stage reference is present, so we're already added to the 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;
}
//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 = 15.65;
wciecie2 = 20.05;
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;
}
}
}
}
}
Hope it will helps.

I expect you are getting an error from the init function which expects lots of parameters but might just get one Event. It helps when you post here if you post the compile or runtime errors you are getting along with source code.
I think this should work for you, I've made a rough version which you can learn from and apply to your own class
public class CustomClass extends MovieClip
{
protected var _company:String;
protected var _data:Object;
public function CustomClass( company:String='', data:Object=null )
{
_company = company;
_data = data;
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//do something with _data
//do something with _company
}
}
Hopefully you can see the concept here, place your constructor variables in class variables when you create the class, then if on the stage call init() which uses those class variables or add an event listener which will call init(passing in an event this time) and then use the same class variables to do what you want.
Note how I remove the event listener when it isn't needed any more as well.

Related

Making a reset placed objects button in Flash (as3)

I have a drag and drop project where everytime I click on a movieclip a clone is made that can be dragged. I would like to know how to make a button that can reset/remove the clones when the button is pressed.
This is what I got so far:
import flash.display.MovieClip;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
addChild(latestClone);
latestClone.startDrag();
latestClone.addEventListener(MouseEvent.MOUSE_DOWN, latestClone.startDrag);
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if(latestClone != null){
latestClone.stopDrag();
}
}
Add all of your clones in an array, and loop over the array and do a removeChild on each item in the array. So:
var items:Array = new Array();
....
addChild(latestClone);
items.push(latestClone);
....
var resetButton:SimpleButton = new SimpleButton();
//set your button properties here
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);
function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{
removeChild(items[i]);
items[i] = null;
}
items = new Array();
}
Hope this helps.
just declare container variable like so:
var clone_container:Sprite = new Sprite();
put all clones inside,than u can clear it up very easely:
while(clone_container.numChildren > 0){
clone_container.removeChildAt(0);
}
that's all..
I found the solution to my question by testing several possibilities with the code that was posted + searching other questions.
This is the code that worked:
import flash.display.MovieClip;
import flash.events.MouseEvent;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
addChild(latestClone);
latestClone.startDrag();
latestClone.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
}
function onClonedPlusPressed(event:MouseEvent):void{
latestClone = MovieClip(event.currentTarget);
latestClone.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if(latestClone != null){
latestClone.stopDrag();
items.push(latestClone);
}
}
resetButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
}
var items:Array = new Array();
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);
function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{ items[i].removeEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
if(items[i].parent) items[i].parent.removeChild(items[i]);
items[i] = null;
}
items = new Array()
}
you most add an array and push object into this,
your solution:
import flash.display.MovieClip;
import flash.events.Event;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
var plusArray:Array = new Array();
resetbtn.addEventListener(MouseEvent.CLICK,resetFunc);
function resetFunc(e:Event)
{
for (var i=0; i<plusArray.length; i++)
{
removeChild(plusArray[i]);
}
plusArray = new Array()
}
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
plusArray.push(latestClone);
addChild(plusArray[plusArray.length-1]);
plusArray[plusArray.length - 1].startDrag();
plusArray[plusArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN, plusArray[plusArray.length-1].startDrag);
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if (latestClone != null)
{
latestClone.stopDrag();
}
}
Good luck

Creating obstacles in AS3

I'm having troubles with creating obstacles in Flash. I managed somehow to create obstacles but are only visible if there is no background. When adding one they simply disappear...
Here's the code for the main.as file:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.utils.setInterval;
import flash.utils.clearInterval;
public class Main extends MovieClip {
private var obstacleArray:Array;
private var obstacleInterval:uint;
private var rural2:Rural2;
private var rural3:Rural2;
private var cityFront2:CityFront2;
private var cityFront3:CityFront2;
private var cityBack2:CityBack2;
private var cityBack3:CityBack2;
private var skyline:Skyline;
private var skyline2:Skyline;
public var ship:Ship;
public function Main() {
// constructor code
obstacleArray = new Array();
obstacleInterval = setInterval(createObstacle, 1000);
addSkylineToStage();
addCityBack2ToStage();
addCityFront2ToStage();
addRural2ToStage();
createShip();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function addSkylineToStage() {
skyline = new Skyline();
/*var myBlurFilter:BlurFilter = new BlurFilter(8,8);
skyline.filters = [myBlurFilter];*/
stage.addChild(skyline);
skyline2 = new Skyline();
skyline2.x = skyline.width;
stage.addChild(skyline2);
}
private function addCityBack2ToStage() {
cityBack2 = new CityBack2();
cityBack2.y = 310;
cityBack2.x = 20;
var myBlurFilter:BlurFilter = new BlurFilter(3.7,3.7);
cityBack2.filters = [myBlurFilter];
stage.addChild(cityBack2);
cityBack3 = new CityBack2();
cityBack3.y = 310;
cityBack3.x = cityBack2.width-20;
cityBack3.filters = [myBlurFilter];
stage.addChild(cityBack3);
}
private function addCityFront2ToStage() {
cityFront2 = new CityFront2();
cityFront2.y = 310;
var myBlurFilter:BlurFilter = new BlurFilter(2.3,2.3);
cityFront2.filters = [myBlurFilter];
stage.addChild(cityFront2);
cityFront3 = new CityFront2();
cityFront3.y = 310;
cityFront3.x = cityFront2.width-8;
cityFront3.filters = [myBlurFilter];
stage.addChild(cityFront3);
}
private function addRural2ToStage() {
rural2 = new Rural2();
rural2.y = 410;
stage.addChild(rural2);
rural3 = new Rural2();
rural3.y = 410;
rural3.x = rural2.width-2;
stage.addChild(rural3);
}
var position:String = 'bottom';
private function createObstacle(){
if(position == 'bottom'){
position = 'top';
}else{
position = 'bottom';
}
var obstacle:Obstacle = new Obstacle(stage,position);
obstacleArray.push(obstacle);
obstacle.x = stage.stageWidth;
addChild(obstacle);
}
private function stopObstacles(){
for(var i:int = 0; i<obstacleArray.length;i++){
var obstacle:Obstacle = obstacleArray[i];
obstacle.removeEvents();
}
clearInterval(obstacleInterval);
}
private function onEnterFrame(evt:Event) {
rural2.x -= 2;
rural3.x -= 2;
if(rural2.x <= -rural2.width){
rural2.x = rural3.width-4;
}
if(rural3.x <= -rural3.width){
rural3.x = rural2.width-4;
}
cityFront2.x -= 1;
cityFront3.x -= 1;
if(cityFront2.x <= -cityFront2.width){
cityFront2.x = cityFront3.width-15;
}
if(cityFront3.x <= -cityFront3.width){
cityFront3.x = cityFront2.width-15;
}
cityBack2.x -= 0.5;
cityBack3.x -= 0.5;
if(cityBack2.x <= -cityBack2.width){
cityBack2.x = cityBack3.width-50;
}
if(cityBack3.x <= -cityBack3.width){
cityBack3.x = cityBack2.width-50;
}
skyline.x -= 0.25;
skyline2.x -= 0.25;
if(skyline.x <= -skyline.width){
skyline.x = skyline2.width-2;
}
if(skyline2.x <= -skyline2.width){
skyline2.x = skyline.width-2;
}
for(var i:int = 0; i<obstacleArray.length; i++){
var obstacle:Obstacle = obstacleArray[i];
if(obstacle.hitTestObject(ship)){
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
stopObstacles();
}
}
}
public function createShip() {
ship = new Ship(stage);
stage.addChild(ship);
ship.x = 20;
ship.y = 180;
}
}
}
And the code for obstacles.as file:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Obstacle extends MovieClip {
private var speed:Number = 3;
private var mPosition:String;
private var mStage:Stage;
public function Obstacle(stage:Stage, position:String = 'bottom') {
// constructor code
mPosition = position;
mStage = stage;
addEvents();
setPosition();
}
private function setPosition(){
var factor:Number = Math.random()+0.3;
this.scaleY = factor;
if(mPosition == 'bottom'){
this.y = mStage.stageHeight - this.height;
}else{
this.y = 0;
}
}
private function addEvents(){
addEventListener(Event.ENTER_FRAME, onFrame);
}
public function removeEvents(){
removeEventListener(Event.ENTER_FRAME, onFrame);
}
private function onFrame(evt:Event){
this.x -= speed;
}
}
}
How can I tweak the code, so both the parallax background and obstacles will be seen on the stage?
Thank you!
Oh, initially I missed the "setInterval" in your code which should indeed add your obstacles over the other elements. Maybe try setting their index to the top when they get added? So:
addChildAt(obstacle,stage.numChildren-1)
For testing layering issues, you can try setting all your elements alpha to 0.5 so you can see if something is on the stage but behind other elements or just not getting added to the stage.
Also, http://www.monsterdebugger.com is really helpful for debugging display list issues because you can select elements in the debugger and see their location on the screen.

How do I access a method of ClassA from classB?

I didn't really know what exactly to ask in the question. So I am going to explain everything here:
I am working on a Desktop AIR Database Application I have a Class named "Database Screen" which is linked to a movie clip:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.InteractiveObject;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
public class DatabaseScreen extends MovieClip {
private var allButtons:Array;
private var codeSelected:Boolean;
private var nameSelected:Boolean;
private var xmlDatabase:XML;
private var dbFile:File;
public var xmlList:XMLList;
private var totalProducts:uint;
private var totalItems:uint = 0;
private var productExists:Boolean;
private var productId:Number;
private var XMLReader:xmlReader;
public function DatabaseScreen() {
allButtons = [searchPanel.search_btn, addNewPanel.addNew_btn, dbStatsPanel.xls_btn, searchPanel.code_radio, searchPanel.name_radio];
init();
}
private function init():void
{
XMLReader = new xmlReader();
//adding event listeners..
for(var i:int = 0; i<allButtons.length; i++)
{
allButtons[i].addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
allButtons[i].addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
allButtons[i].addEventListener(MouseEvent.MOUSE_DOWN, onDown, false, 0, true);
allButtons[i].addEventListener(MouseEvent.MOUSE_UP, onUp, false, 0, true);
allButtons[i].addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
allButtons[i].buttonMode = true;
}
//creating a new file for the database
dbFile = File.documentsDirectory.resolvePath("BlackCat/Database.xml");
xmlDatabase = new XML();
getXML();
saveXML();
getTotalItems();
dbStatsPanel.items.text = String(totalItems);
}
private function getXML():void
{
if(dbFile.exists)
{
var stream:FileStream = new FileStream();
stream.open(dbFile, FileMode.READ);
xmlDatabase = new XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
xmlList = xmlDatabase.Product;
dbStatsPanel.products.text = xmlList.length();
XMLReader.xmlData = xmlList;
XMLReader.updateXMLList(xmlList);
}
else
{
xmlDatabase = <Products></Products>
}
}
public function getXMlList():XMLList
{
return xmlList;
}
private function getTotalItems():void
{
for(var i:int = 0; i < xmlList.length(); i++)
{
totalItems += uint(xmlList[i].#Qty);
}
}
private function saveXML():void
{
var stream:FileStream = new FileStream();
stream.open(dbFile, FileMode.WRITE);
stream.writeUTFBytes(xmlDatabase.toXMLString());
}
private function onDown(evt:MouseEvent):void
{
evt.target.scaleX = evt.target.scaleY = .8;
}
private function onUp(evt:MouseEvent):void
{
evt.target.scaleX = evt.target.scaleY = 1;
}
private function onOver(evt:MouseEvent):void
{
evt.target.alpha = .5;
}
private function onOut(evt:MouseEvent):void
{
evt.target.alpha = 1;
evt.target.scaleX = evt.target.scaleY = 1;
}
private function onClick(evt:MouseEvent):void
{
switch(evt.target.name)
{
case "code_radio":
codeSelected = true;
nameSelected = false;
searchPanel.code_radio.gotoAndStop(1);
searchPanel.name_radio.gotoAndStop(2);
break;
case "name_radio":
nameSelected = true;
codeSelected = false;
searchPanel.code_radio.gotoAndStop(2);
searchPanel.name_radio.gotoAndStop(1);
break;
case "addNew_btn":
//Checking if the product already exists..
for(var i:int = 0; i < xmlList.length(); i++)
{
if(xmlList[i].#code == addNewPanel.add_code_txt.text)
{
productExists = true;
productId = i;
}
}
if(productExists)
{
var PQty:uint = uint(xmlList[productId].#Qty);
var PQtoAdd:uint = uint(addNewPanel.add_qty_txt.text);
PQty += PQtoAdd;
xmlList[productId].#Qty = String(PQty);
productExists = false;
}
else
{
xmlDatabase.appendChild(<Product code={addNewPanel.add_code_txt.text}
name={addNewPanel.add_name_txt.text} Price={addNewPanel.add_price_txt.text}
Qty={addNewPanel.add_qty_txt.text}></Product>);
}
totalItems += uint(addNewPanel.add_qty_txt.text);
xmlList = xmlDatabase.Product;
dbStatsPanel.products.text = xmlList.length();
dbStatsPanel.items.text = String(totalItems);
XMLReader.updateXMLList(xmlList);
saveXML();
trace(xmlDatabase);
break;
}
}
public function setVisiblity(visibility:Boolean):void
{
switch(visibility)
{
case true:
this.visible = true;
break;
case false:
this.visible = false;
break;
}
}
public function getXMLList():XMLList
{
return xmlList;
}
}
}
And I have another class named "NewOrder", in this class I am trying to access the xmlList from the "DatabaseScreen" class but I am unable to do so.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import com.greensock.TweenMax;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
public class NewOrder extends MovieClip {
private var allButtons:Array;
private var shoppingCart:Array;
private var scroller:FlickScroll;
private var db:DatabaseScreen;
private var XMLReader:xmlReader;
public function NewOrder() {
this.visible = false;
init(db);
}
private function init(dbase:DatabaseScreen):void
{
XMLReader = new xmlReader();
trace(XMLReader.xmlData);
scroller = new FlickScroll(masker, container, 84.05, -500);
addChild(scroller);
allButtons = [scan_btn, print_invoice];
shoppingCart = [];
for(var i:int = 0; i < allButtons.length; i++)
{
allButtons[i].addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
allButtons[i].addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
allButtons[i].addEventListener(MouseEvent.MOUSE_DOWN, onDown, false, 0, true);
allButtons[i].addEventListener(MouseEvent.MOUSE_UP, onUp, false, 0, true);
allButtons[i].mouseChildren = false;
allButtons[i].buttonMode = true;
}
}
private function onOver(evt:MouseEvent):void
{
evt.target.alpha = .5;
}
private function onOut(evt:MouseEvent):void
{
evt.target.alpha = 1;
evt.target.scaleX = evt.target.scaleY = 1;
}
private function onDown(evt:MouseEvent):void
{
evt.target.scaleX = evt.target.scaleY = .8;
}
private function onUp(evt:MouseEvent):void
{
evt.target.scaleX = evt.target.scaleY = 1;
if(evt.target.name == "scan_btn")
{
var cartElement:CartElement = new CartElement();
container.addChild(cartElement);
cartElement.x = 0;
cartElement.alpha = 0;
TweenMax.to(cartElement, .4, {alpha:1});
shoppingCart.push(cartElement);
cartElement.id_txt.text = String(shoppingCart.length) + " - ";
if(shoppingCart.length < 2)
{
cartElement.y = container.height - 30;
}
else
{
cartElement.y = container.height + 5;
}
}
}
}
}
And there is a Main class in which I handle all the screens , e.g DatabaseScreen, NewOrder.. Which means I have added these to the display list in the Main class. To access the xmlList from the DatabaseScreen , I tried creating a new instance in the NewOrder class, but it creates to DatabaseScreen(s). I also tried creating another class named "xmlReader":
package {
public class xmlReader {
public var xmlData:XMLList;
public function xmlReader() {
}
public function updateXMLList(updatedList:XMLList):void
{
xmlData = updatedList;
}
}
}
In the databaseScreen class, as soon as I populate the xmlList , I set the xmlData of the xmlReader class equal to the xmlList from the databaseScreen class. And when I trace the XMLReader.xmlData in the databaseScreen , it works fine . But when I do the same thing in the NewOrder class, I get "null" in the output window.
How do I solve this problem. Any help is greatly appreciated.
You will need to pass a reference into the Order class to have access to the DatabaseScreen class. Something like this.
var ds = new DatabaseScreen();
var or = new NewOrder(ds)
Inside the NewOrder you can save the ref.
public class NewOrder {
private var screens:DatabaseScreens;
public function NewOrder(ds:DatabaseScreens) {
screens = ds;
// Now you can call screens.xmlList
}
}

how to insert sound to this code?

hello how to put sounds to this code so when tower firing bullet sound will show up together with it...i'm just nubie so i really need a help
can you help me how to put sounds in this code
this is the code
package Game
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import Gamess.BulletThree;
import Games.BulletTwo;
public class Tower_Fire extends MovieClip
{
private var isActive:Boolean;
private var range,cooldown,damage:Number;
public function Tower_Fire()
{
isActive = false;
range = C.TOWER_FIRE_RANGE;
cooldown = C.TOWER_FIRE_COOLDOWN;
damage = C.TOWER_FIRE_DAMAGE;
this.mouseEnabled = false;
}
public function setActive()
{
isActive = true;
}
public function update()
{
if (isActive)
{
var monsters = GameController(root).monsters;
if (cooldown <= 0)
{
for (var j = 0; j < monsters.length; j++)
{
var currMonster = monsters[j];
if ((Math.pow((currMonster.x - this.x),2)
+ Math.pow((currMonster.y - this.y),2)) < this.range)
{
//spawn new bullet
var bulletRotation = (180/Math.PI)*
Math.atan2((currMonster.y - this.y),
(currMonster.x - this.x));
var newBullet = new Bullet(currMonster.x,currMonster.y,
"fire",currMonster,this.damage,bulletRotation);
newBullet.x = this.x;
newBullet.y = this.y;
GameController(root).bullets.push(newBullet);
GameController(root).mcGameStage.addChild(newBullet);
this.cooldown = C.TOWER_FIRE_COOLDOWN;
break;
}
}
}
else
{
this.cooldown -= 1;
}
}
}
}
}
Firstly, you need to export sound for AS:
and then you can play sound from code:
var sound:Sound = new Track();
sound.play();
UPDATE:
if ((Math.pow((currMonster.x - this.x),2) + Math.pow((currMonster.y - this.y),2)) < this.range)
{
//spawn new bullet
var sound:Sound = new Track();
sound.play();
var bulletRotation = (180/Math.PI)*
Math.atan2((currMonster.y - this.y), (currMonster.x - this.x));
...

Need to know which sprite was clicked

This is my level selector for a game. You can choose level 1-8, I need to know which one was clicked. e.target doesn't give me much besides the name of the object. I need to know either the place in the array or the name of the picture so I know which level to load up.
package
{
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class LevelSelector extends Sprite
{
private var posX:int = 50;
private var posY:int = 50;
[Embed(source="../lib/one.png")]
private var lvl1Class:Class;
private var lvl1:Bitmap = new lvl1Class();
[Embed(source="../lib/two.png")]
private var lvl2Class:Class;
private var lvl2:Bitmap = new lvl2Class();
[Embed(source="../lib/three.png")]
private var lvl3Class:Class;
private var lvl3:Bitmap = new lvl3Class();
[Embed(source="../lib/four.png")]
private var lvl4Class:Class;
private var lvl4:Bitmap = new lvl4Class();
[Embed(source="../lib/five.png")]
private var lvl5Class:Class;
private var lvl5:Bitmap = new lvl5Class();
[Embed(source="../lib/six.png")]
private var lvl6Class:Class;
private var lvl6:Bitmap = new lvl6Class();
[Embed(source="../lib/seven.png")]
private var lvl7Class:Class;
private var lvl7:Bitmap = new lvl7Class();
[Embed(source="../lib/eight.png")]
private var lvl8Class:Class;
private var lvl8:Bitmap = new lvl8Class();
private var myArrayBitmaps:Array = new Array;
private var myArraySprites:Array = new Array;
private var yCounter:int = 1;
public function LevelSelector():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
myArrayBitmaps.push(false, lvl1, lvl2, lvl3, lvl4, lvl5, lvl6, lvl7, lvl8);
for (var i:int = 1; i < 9; i++)
{
myArrayBitmaps[i].x = posX;
myArrayBitmaps[i].y = posY;
myArrayBitmaps[i].height = 50;
myArrayBitmaps[i].width = 100;
//myArrayBitmaps[i].name = "lvl_" + i + "";
myArraySprites[i] = new Sprite();
myArraySprites[i].addChild(myArrayBitmaps[i]);
myArraySprites[i].x = posX;
myArraySprites[i].y = posY;
//myArraySprites[i].name = "lvl_" + i + "";
stage.addChild(myArraySprites[i]);
myArraySprites[i].addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
removeThis(e, this);
});
posX += 100;
yCounter += 1;
if (yCounter == 5) {
posY += 100;
posX -= 400;
}
}
}
private function removeThis(e:MouseEvent, temp):void
{
//Need to know the number which was passed, 1-8
}
}
}
I would do something like this:
//...
private function init(e:Event = null):void
{
//...
for (var i:int = 1; i < 9; i++)
{
//...
myArraySprites[i] = new Sprite();
myArraySprites[i].addChild(myArrayBitmaps[i]);
//...
stage.addChild(myArraySprites[i]);
myArraySprites[i].addEventListener(MouseEvent.CLICK, removeThis);
}
}
}
private function removeThis(e:MouseEvent):void
{
var clickTarget:int = myArraySprites.indexOf(e.currentTarget);
trace("Clicked sprite (id): " + clickTarget);
}