Isn't this how to specify a passing information custom event? - actionscript-3

This game is a board game so it need to pass information but mostly not dynamically.
Is this the correct definition of a Custom Event?
package
{
import flash.events.Event;
public class Set extends Event
{
public var addsub:Boolean;
public var kanaex:String;
public var valueex:uint;
public var xx:uint;
public var yy:uint;
public static const BOARD_SET_CHANGED:String = "BoardSetChanged";
public static const BOX_SET_CHANGED:String = "BoxSetChanged";
public function Set(type:String, addsub1:Boolean, kanaex1:String, valueex1:uint, xx1:uint,yy1:uint, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
addsub = addsub1;
kanaex = kanaex1;
valueex= valueex1;
xx = xx1;
yy = yy1;
}
override public function clone():Event
{
return new Set(type, addsub, kanaex,valueex,xx,yy, bubbles, cancelable );
}
override public function toString():String
{
return formatToString("BoardSetChanged","addsub","kanaex","valueex","xx","yy","bubbles", "cancelable");
}
}
}
If so why does this code leave the
event attributes undefined?
import Set;
import flash.events.*;
this.addEventListener(Set.BOARD_SET_CHANGED, Exclusion);
private function Exclusion(e:Event)
{
var a:Boolean = e.addsub;
var b:uint = e.xx;
var c:uint = e.yy;
if (a == true)
{exclusionx.push(b);
exclusiony.push(c);
}
else if (a == false)
{exclusionx.pop();
exclusiony.pop();
}
}

Try to change the parameter of Exclusion to Set
private function Exclusion(e:Set):void {
}

Related

As3 add Checkbox in Grid component

I want to add dynamic checkbox in a grid componemt in AS3. Here is the link. I want to do it in Flash AS3.0 not in Flex.
For custom cells you need to you use custom cellRenderer on the column.
Main
var sampleItem1:Object = {Name:"John Alpha", Number:"555-123-0101", Email:"jalpha#fictitious.com"};
var dg:DataGrid = new DataGrid();
var dgc:DataGridColumn = new DataGridColumn("Name");
dg.addColumn(dgc);
dgc = new DataGridColumn("Number");
dgc.cellRenderer = "CustomCellRenderer"; // here you set your custom renderer for this column
dg.addColumn(dgc);
dgc = new DataGridColumn("Email");
dg.addColumn(dgc);
dg.addItem(sampleItem1);
CheckBoxCellRenderer.as
package
{
import fl.controls.CheckBox;
import fl.controls.listClasses.ICellRenderer;
import fl.controls.listClasses.ListData;
public class CustomCellRenderer extends CheckBox implements ICellRenderer {
private var _listData:ListData;
private var _data:Object;
public function CustomCellRenderer() {
}
public function set data(d:Object):void {
_data = d;
label = d.label;
}
public function get data():Object {
return _data;
}
public function set listData(ld:ListData):void {
_listData = ld;
}
public function get listData():ListData {
return _listData;
}
}
}
source1 source2

Get data from two object in OOP AS3

I have a Car class like this
public class Car extends Sprite
{
private var car :Sprite;
private var buttonCar :Sprite;
private var _kmh :int;
public function Car()
{
makeCar();
makeButtonCar();
}
private function makeCar() : void
{
car = new Sprite();
car.graphics.beginFill(0x0000FF, 1);
car.graphics.drawRect(0, 0, 100, 50);
car.x = 100;
this.addChild(car);
}
private function makeButtonCar() : void
{
buttonCar = new Sprite();
buttonCar.graphics.beginFill(0xFF0000, 1);
buttonCar.graphics.drawCircle(0, 0, 25);
buttonCar.x = 300;
this.addChild(buttonCar);
buttonCar.addEventListener(MouseEvent.MOUSE_DOWN, KMH);
}
private function KMH(e:MouseEvent) : void
{
_kmh++;
trace("kmh: "+_kmh);
}
}
in the Main class I make newCar from Car class, newCar1 and newCar2.
public class OOPVariable extends Sprite
{
private var newCar1  :Car;
private var newCar2  :Car;
public function OOPVariable()
{
newCar1 = new Car();
addChild(newCar1);
newCar2 = new Car();
newCar2.y = 100; 
addChild(newCar2);
super();
}
}
I want get total of variable _kmh from all object newCar when one of the button from newCar clicked and mouse event still in Car class.
you could either do what Nathan said, or you can make your kmh variable public;
that way you can access it through a mouseEvent.
first, you'll have to import flash.utils.getQualifiedClassName.
then you can use a for() loop to get all of the cars on stage, and trace the total KMH.
// traces out the total KMH and num of cars on stage
// your car class should keep kmh updated
private function getTotalKMH(e:MouseEvent) :void
{
var totalCars:int = 0;
var KMH:int = 0;
for (var i:int = 0; i < stage.numChildren-1; i++)
{
if (getQualifiedClassName(e.currentTarget) == "Car")
{
++totalCars;
KMH += getChildByIndex(i).kmh; // ".kmh" is the variable in your car class
}
}
trace("Total Cars: " + totalCars + "\nTotal KMH: " + KMH);
}
of course, you can do more than just trace it.
you can pass it to a class scope variable or a function if you need to.
thanks all for your reply, I'm using custom event and dispatch event like what Nathan said :D
this is Main class, carListener method summing all kmh from all car when button car clicked
package
{
import flash.display.Sprite;
import support.CarEvent;
import support.CarObject;
public class OOPCar extends Sprite
{
private var newCar1 :CarObject;
private var newCar2 :CarObject;
private var totalKmh :int = 0;
private var currentName :String;
public function OOPCar()
{
newCar1 = new CarObject();
newCar1.name = "car1";
newCar1.setKmh(2);
addChild(newCar1);
newCar1.addEventListener(CarEvent.UPDATE, carListener);
newCar2 = new CarObject();
newCar2.name = "car2";
newCar2.setKmh(4);
addChild(newCar2);
newCar2.addEventListener(CarEvent.UPDATE, carListener);
newCar2.y = 100;
}
private function carListener(e:CarEvent) : void
{
trace("kmhCar: "+e.kmhCar);
totalKmh += e.kmhCar;
trace("totalKmh: "+totalKmh);
currentName = e.currentTarget.name;
}
}
}
class for make a car
package support
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class CarObject extends Sprite
{
private var car :Sprite;
private var buttonCar :Sprite;
private var _kmh :int;
public function CarObject()
{
makeCar();
makeButtonCar();
}
private function makeCar() : void
{
car = new Sprite();
car.graphics.beginFill(0x0000FF, 1);
car.graphics.drawRect(0, 0, 100, 50);
car.x = 100;
this.addChild(car);
}
private function makeButtonCar() : void
{
buttonCar = new Sprite();
buttonCar.graphics.beginFill(0xFF0000, 1);
buttonCar.graphics.drawCircle(0, 0, 25);
buttonCar.x = 300;
this.addChild(buttonCar);
buttonCar.addEventListener(MouseEvent.MOUSE_DOWN, update);
}
public function setKmh(kmh:int) : void
{
_kmh = kmh;
}
public function update(e:MouseEvent) : void
{
dispatchEvent(new CarEvent(CarEvent.UPDATE, true, false, _kmh));
}
}
}
and this is my custom event
package support
{
import flash.events.Event;
public class CarEvent extends Event
{
public static const UPDATE:String = "update";
public var kmhCar :int;
public function CarEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, kmhCar:int = 0)
{
super(type, bubbles, cancelable);
this.kmhCar = kmhCar;
}
public override function clone() : Event
{
return new CarEvent(type, bubbles, cancelable, kmhCar);
}
public override function toString():String
{
return formatToString("CarEvent", "type", "bubbles", "cancelable", "eventPhase", "kmhCar");
}
}
}

AS3 TypedDictionary?

public dynamic class TypedDictionary extends Dictionary {
private var _type:Class;
public function TypedDictionary(type:Class, weakKeys:Boolean = false) {
_type = type;
super(weakKeys);
}
public function addValue(key:Object, value:_type):void {
this[key] = value;
}
public function getValue(key:Object):_type{
return this[key];
}
...
I want to make TypedDictionary with typisation. But I can not use _type in addValue and getValue. The idea is to use next construction :
var td:TypedDictionary = new TypedDictionary(myClass, true);
td.addValue("first", new myClass());
...
var item:myClass = td.getValue("first");
item.someFunction();
Is any ability to use dynamic class type?
If I un derstand what you are asking for.
This is untested code so it could be error prone, but it should get you on the right path.
public dynamic class TypedDictionary extends Dictionary {
private var _type:Class;
public function TypedDictionary(type:String, weakKeys:Boolean = false) {
_type = getDefinitionByName(type) as Class;
super(weakKeys);
}
public function addValue(key:Object, value:*):void {
if(_type == getDefinitionByName(getQualifiedClassName(value))){
this[key] = value;
}else{
trace('failed type match')
}
}
public function getValue(key:Object):*{
return this[key];
}
...
var td:TypedDictionary = new TypedDictionary("com.somepackage.myClass", true);
td.addValue("first", new myClass());
var item:myClass = td.getValue("first");
item.someFunction();

can i reach a main class variable, by using the value of an external class property

Ok so my problem is this.
In my main class I have a Boolean type variable. In the external class I have a String type variable.
Is it possible to access the variable in my main class, by using the string value of the variable in my external class. Note that the string value of the external class property matches the main class variable.
I just tryed doing this:
Main class CardGame.as has a variable var slot1:Boolean.
In the external class there is the variable var slot:String = slot1;
I also have this line of code: CardGame['slot'] = false;
It doesn't seem to be working :( . Any help would be appreciated. Thanks
Part of the main class file:
function drawCard():void
{
var card:Card = new Card();
if(slot1 == false)
{
card.x = 30;
slot1 = true;
card.slot = "slot1";
}
else if(slot2 == false)
{
card.x = 190;
slot2 = true;
card.slot = "slot2";
}
else if(slot3 == false)
{
card.x = 350;
slot3 = true;
card.slot = "slot3";
}
else if(slot4 == false)
{
card.x = 510;
slot4 = true;
card.slot = "slot4";
}
else if(slot5 == false)
{
card.x = 670;
slot5 = true;
card.slot = "slot5";
}
else
{
card.x = 830;
slot6 = true;
card.slot = "slot6";
}
card.y = cardY;
cardContainer.addChild(card);
}
And the external file:
import flash.display.MovieClip;
import flash.events.MouseEvent;
import CardGame;
public class Card extends MovieClip
{
public var slot:String;
public function Card()
{
// constructor code
addEventListener(MouseEvent.CLICK, removeCard)
}
function removeCard(event:MouseEvent):void
{
this.parent.removeChild(this);
CardGame['slot'] = false;
}
}
You'll need a few lines of code in the external class:
// in CardGame.as
// declare slot1 as a public var right after the class declaration to insure the
// correct scope
public class CardGame extends MovieClip {
public static var slot1:Boolean;
....
// in external class
import CardGame // depends on where this is in relation to the external class
function theFunction():void {
// somewhere in your external class
var slot:String = CardGame.slot1.toString();
}
// Example classes that show how it works
// Main Class - instantiates ExtClass
package {
import flash.display.Sprite;
public class MainVar extends Sprite {
public static var slot1:Boolean;
private var extClass:ExtClass;
public function MainVar() {
slot1 = true;
this.extClass = new ExtClass();
}
}
}
// External Class accesses static var, modifies static var
package {
public class ExtClass {
public function ExtClass() {
var slot:String = MainVar.slot1.toString();
var index:String = "1";
var slotVar:String = "slot" + index;
trace(slot);
// get the value using a string
trace("String access: ", MainVar[slotVar])
MainVar.slot1 = false;
slot = MainVar.slot1.toString();
trace("Var access: ", slot);
// get the value using a string
trace("String access: ", MainVar[slotVar]);
}
}
}

How to dispach a custom event inside httpservice result event

In my AIR application, I try to dispatch a custom event from a class to main window.
This class is use to call httpservice. My goal is to send a custom window when the httpservice result is send.
package fr.inter.DataProvider
{
import flash.events.Event;
import flash.events.EventDispatcher;
import fr.inter.config.urlManager;
import fr.kapit.introspection.components.DisplayListComponent;
import mx.collections.XMLListCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
[Event(name="evtPatSelect", type="flash.events.Event")]
public class sPatient
{
private var _phppaIndex:String;
private var _phppaNomU:String;
private var _phppaPrenom:String;
private var phpSearchPatNom:HTTPService;
public function sPatient()
{
}
public function sPhpSearchPat(p:Object):void
{
phpSearchPatNom = new HTTPService();
phpSearchPatNom.method="POST";
phpSearchPatNom.resultFormat = "e4x";
phpSearchPatNom.addEventListener(ResultEvent.RESULT,resultListePatient);
phpSearchPatNom.addEventListener(FaultEvent.FAULT,serviceFault);
var urlPhp:urlManager=new urlManager();
phpSearchPatNom.url = urlPhp.urlService() + "20SearchNom.php";
phpSearchPatNom.send(p);
}
private function resultListePatient( event:ResultEvent ):void
{
var xmlList:XMLList = XML(event.result).patientPHP;
var xmlListColl = new XMLListCollection(xmlList);
if(xmlListColl.length==1)
{
_phppaIndex = xmlListColl.getItemAt(0).paIndex;
_phppaNomU = xmlListColl.getItemAt(0).paNomU;
_phppaPrenom = xmlListColl.getItemAt(0).paPrenom;
var evtPat:Event = new Event("evtPatSelect");
var evdips:EventDispatcher = new EventDispatcher();
evdips.dispatchEvent(evtPat);
}
}
private function serviceFault( event:FaultEvent )
{
trace( event.fault.message );
}
public function get phppaIndex():String
{
return _phppaIndex;
}
public function set phppaIndex(value:String):void
{
_phppaIndex = value;
}
public function get phppaNomU():String
{
return _phppaNomU;
}
public function set phppaNomU(value:String):void
{
_phppaNomU = value;
}
public function get phppaPrenom():String
{
return _phppaPrenom;
}
public function set phppaPrenom(value:String):void
{
_phppaPrenom = value;
}
}
}
In main window I've added a eventlistener but this seems not works.
Can you help me to solve that?
Thanks
First, extend the sPatient class as an EventDispatcher
public class sPatient extends EventDispatcher {
Then, create a class for your custom Event
public class MyCustomEvent extends Event {
public static const CUSTOM_TITLE:String = "custom_title";
public var eventData:Object;
public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, data:Object = null) {
super(type, bubbles, cancelable);
if(data != null) eventData = data;
}
public override function clone():Event {
return new CustomEvent(type, bubbles, cancelable);
}
public override function toString():String {
return formatToString("CustomEvent", "type", "bubbles", "cancelable", "eventPhase");
}
}
Then in your sPatient class, go:
this.dispatchEvent(new MyCustomEvent(MyCustomEvent.CUSTOM_TITLE));
And listen for it like so
sPatientInstance.addEventListener(MyCustomEvent.CUSTOM_TITLE,functionHandler);