AS3. MouseEvent click in (for) loop function - actionscript-3

am need here onClick one of the movieclip loop_Task.addChild(tee) trace the tee.task_id.text which one is clicked from the list for ex. this
output be like
(List Clicked : 100)
(List Clicked : 101)
or onClick passed the item data it's already clicked to new class screen
here is my code
public function resultHandlerList_items(event:SQLEvent):void
{
// TODO Auto-generated method stub
var result:SQLResult = selectStmt1.getResult();
var numResults:int = result.data.length;
for (var i:int = 0; i < numResults; i++)
{
var row:Object = result.data[i];
var tee:listview_mc = new listview_mc
loop_Task.addChild(tee)
tee.y = 270*i
tee.task_id.text = row.Tid
tee.task_tit.text = row.Ttitles
tee.task_stime.text = row.Stime
tee.task_subject.text = row.Subject
tee.addEventListener(MouseEvent.CLICK, onClickList)
}
function onClickList(e:MouseEvent):void{
trace("List Clicked : " + e.currentTarget)
}
}

in the first thanks to #Organis He give me the way to fix my code and he give me info about how to talk with the compiler with exactly how to make the compiler understand your code this code the fix (e.currentTarget as listview_mc) and now this my code after fixing and i do take a public var String and pass it to other Class
public function resultHandlerList_items(event:SQLEvent):void
{
// TODO Auto-generated method stub
var result:SQLResult = selectStmt1.getResult();
var numResults:int = result.data.length;
for (var i:int = 0; i < numResults; i++)
{
var row:Object = result.data[i];
var tee:listview_mc = new listview_mc
loop_Task.addChild(tee)
tee.y = 270*i
tee.task_id.text = row.Tid
tee.task_tit.text = row.Ttitles
tee.task_stime.text = row.Stime
tee.task_subject.text = row.Subject
tee.addEventListener(MouseEvent.CLICK, onClickList)
}
function onClickList(e:MouseEvent):void{
trace("List Clicked : " + (e.currentTarget as listview_mc).task_id.text)
ts_id = (e.currentTarget as listview_mc).task_id.text;
sport._sport.Remove_Home_Sc(e);
trace("done")
}
}

Related

how addChild is being used in below AS3 code?

For addChild(iconObject) (second line inside function startAmoebaAttack()), can anyone explain me where will iconObject be added in? because it doesn't have any object or array before addChild like iconObject.addChild(newSoldier);
private var iconObject:Sprite;
public function startAmoebaAttack() {
iconObject = new Sprite();
addChild(iconObject);
createSoldierIcon();
}
public function createSoldierIcon(){
soldierIcon = new Array();
for(var i:uint = 0; i<soldierLeft; i++){
var newSoldier:SoldierIcon = new SoldierIcon();
newSoldier.x = 65 + i *24;
newSoldier.y = 590;
iconObject.addChild(newSoldier);
soldierIcon.push(newSoldier);
}
}
It will automatically run it in this's context (same as this.addChild(iconObject)). In your case this is descendant of DisplayObjectContainer .

How do i change an array based on another text field?

i have a TheList.as class..in which i have created a list for an android app.
Here is TheList.as(Cut down to the specific stuff for this question)
public var _ListItem:ListItem;
public var _Data:Array;
public var _Values:Array;
public var $CurrentValue:String;
public var _TextLabel:TextField;
public function TheList(Data:Array,Values:Array)
{
_Data = Data;
_Values = Values;
initialize();
}
private function initialize():void
{
_TextLabel = new TextField();
addChild(_TextLabel);
_TextLabel.text = "Data";
_Container = new ListContainer ;
addChild(_Container);
_Container.x = 0;
_Container.y = 0;
currentY = _Container.y;
lastY = _Container.y;
for (var i:int = 0; i < _Data.length; i++)
{
_ListItem = new ListItem ;
_Container.addChild(_ListItem);
_ListItem.y = _ListItem.height * i;
_ListItem.addEventListener(MouseEvent.MOUSE_DOWN,onItemDown,false,0,true);
_ListItem.addEventListener(MouseEvent.MOUSE_UP,onItemUp,false,0,true);
_ListItem.mouseChildren = false;
_ListItem.value = _Values[i];
_ListItem.name = _Data[i];
_ListItem.ItemLabel.text = _ListItem.name ;
}
}
Here is the class "TheList" is being used in (Again Cut Down to specific stuff)
$myList = new TheList($Data,$Values);
addChild($myList);
$myList.x = -240;
$myList.y = -203;
$myList.visible = false;
$ListFrom = new TheList($DataFromTo, $ValuesFromTo);
addChild($ListFrom);
$ListFrom.x = -240;
$ListFrom.y = -203;
$ListFrom.visible = false;
$ListFrom._TextLabel.text = $DataFromTo[0];
$ListTo = new TheList($DataFromTo, $ValuesFromTo);
addChild($ListTo);
$ListTo.x = -240;
$ListTo.y = -203;
$ListTo.visible = false;
$ListTo._TextLabel.text = $DataFromTo[0];
Now what i am trying to achieve is that i want to change "$Data" and "$Values" Arrays..as you can see i have a main list and two sublists... when "Time" is selected in the main list, i want the sub-lists to be populated with "Time" related unit names...i tried
if($myList._TextLabel.text == "Time")
{
$ListFrom._Data = ["this", "this", "this" etc]
}
But its not working. I am not getting any error either. I'd really appreciate any help!
What you try to achieve is not pissible in this way. You initialize you lists with data fields. When you reset the data, you need to invalidate your list again. You can achieve this with an item setter
private var __Data:Array;
public function get _Data():Array
{
return __Data;
}
public function set _Data(value:Array):void
{
this.__Data = value;
initialize();
}
make sure that you first cleanup you list before you rebuild it again.
your code:
$ListFrom._Data = ["this", "this", "this" etc]
will automaticly call the setter. A getter and setter method looks like a function but is invoked like a property. means:
object.property = "something" //will call the setter
var something:String = object.property //will call the getter

AS3: Casting to Vector

I am trying to create a vector from unknown class, but it fails, any ideas about how to do it?
This is what i tried:
var vector:Vector = new Vector(); // throw exception
function base():void{
var vector:Vector.<String> = createVector(String);// throw classCastException
}
function createVector(cls:Class):*{
var array:Array = new Array();
for(var i:int = 0; i < 10; i++){
var element:cls = new cls();
array.push(element);
}
return Vector(array);
}
Vector is expecting a parameter type so you can't do this like you want, but using getQualifiedClassName to get class info you can construct a string that will enable you to call the Vector. constructor using getDefinitionByName :
Ex.
// get class parameter name
var className:String=getQualifiedClassName(String);
// get the Vector class object for the given class
var o:Object=getDefinitionByName("__AS3__.vec::Vector<"+className+">");
// call the constructor
var v:*=o.prototype.constructor(["hello", "world"]);
So your function can be written as:
public function createVector(cls:Class):*{
var cn:String = getQualifiedClassName(cls);
var o:Object = getDefinitionByName("__AS3__.vec::Vector.<"+cn+">");
var array:Array = [];
for(var i:int = 0; i < 10; i++){
var element:* = new cls();
array.push(element);
}
return o.prototype.constructor(array);
}
live example at wonderfl:
http://wonderfl.net/c/pkjs
Based on #Patrick answer I found a working solution.
Check it out:
function createVector(cls:Class):*{
var className:String = getQualifiedClassName(cls);
var vectorClass:Class = getDefinitionByName("__AS3__.vec::Vector.<"+className+">") as Class;
var vector:* = new vectorClass(10);
for(var i:int = 0; i < 10; i++){
var element:MyClass = new cls(); // may be Object or Object extends cls
vector[i] = element;
}
return vector;
}

How to pass argument in addEventListener (Action Script)

How do I pass arguments using ActionScript's event-listener?
I have code, as given below, which creates a label, and I want, when clicked on the label it should pass the toolTip associated with that label.
This is what I was trying to do:
public function create_folderpath():void
{
for(var i:int = 0; i < fm_model.absolute_path_ac.length; i++)
{
var absolutePathToolTip:String = new String;
for(var j:int = 0; j <= i; j++)
{
absolutePathToolTip += fm_model.absolute_path_ac[j].path.toString() + '/';
}
var textItem:Label = new Label();
textItem.data = absolutePathToolTip;
textItem.toolTip = absolutePathToolTip;
textItem.text = fm_model.absolute_path_ac[i].path.toString() + ' /';
textItem.addEventListener(MouseEvent.CLICK, testing)
directoryPathHBox.addChild(textItem);
}
}
public function testing(e:MouseEvent)
var direcoryLabel:Label = e.target as Label;
Alert.show(direcoryLabel.data +"");
}
This does not work, nor do I get any errors.
Please, I need help with this.
Thanks in advance
Zeeshan
Try to use "currentTarget" instead of "target":
var direcoryLabel:Label = e.currentTarget as Label;
Alert.show(direcoryLabel.data +"");
And be sure to add a trace in the listener, to know for sure if it's called or not.

trouble separating loader array and array for grid of images

My problem is that I can't figure out how to decouple the loader from this loadNewRow function. Ideally, I'd like to figure out how to load all the swf's give them dynamic names... like thmb1loaded, thmb2loaded, thmb3loaded.. push into an a ("loaded") array and then have that be used to build the grid.
Right now, its loading them every time the loadnewrow function fires. I'm sure its a simple matter of iterating a .name within a contentloaderinfo .onComplete when each swf is loaded... anyhelp would be appreciated this has been driving me crazy last couple of days..
var filePaths:Array=["thmb1.swf","thmb2.swf","thmb3.swf","thmb4.swf", "thmb5.swf", "thmb6.swf",
"thmb7.swf","thmb8.swf", "thmb9.swf", "thmb10.swf", "thmb11.swf" ];
function loadImages3(event:MouseEvent):void {
TweenLite.to(pic, 8, {alpha:0, ease:Expo.easeInOut});// dim image container
trace("loadImages3 fired ---------------------------------------------------------")
unloadImages3resize(); loadImages3Flag=true;
var randomScale:Number = .3 + Math.random();trace("in loadimages3 function randomScale is now.. " + randomScale);
var stage_w=stage.stageWidth;trace("in loadimages3 function stage_w is = " + stage_w);
var stage_h=stage.stageHeight;trace("in loadimages3 function stage_h is = " + stage_h);
var thumbWidth:Number=240;var thumbHeight:Number=155;//var randomScale:Number=.45;
var scaleThumbs:Number=randomScale;
var finalRandomScaledWidth:Number=thumbWidth*scaleThumbs;
var finalRandomScaledHeight:Number=thumbHeight*scaleThumbs;
var clipsFitX=Math.floor(stage_w/finalRandomScaledWidth);
trace("in loadimages3 function finalRandomScaledWidth is = " + finalRandomScaledWidth);
var clipsFitY=Math.floor(stage_h/finalRandomScaledHeight);
trace("in loadimages3 function finalRandomScaledHeight is = " + finalRandomScaledHeight);
var clipsFitXTotal:Number = clipsFitX+1; // add extra one on the horizontal
var clipsFitYTotal:Number = clipsFitY+2; // add extra two on the vertical
trace ("ClipsFitXtotal) = " +clipsFitXTotal, "(clipsFitYTotal) = " + clipsFitYTotal);
loadnewRow(clipsFitXTotal, clipsFitYTotal,randomScale,finalRandomScaledHeight, finalRandomScaledWidth );
}
function loadnewRow(clipsFitXTotal:Number, clipsFitYTotal:Number, randomScale:Number,
finalRandomScaledHeight:Number, finalRandomScaledWidth:Number):void {
trace("loadnewRow fired ---------------------------------------------------------");
trace("randomScale out of loop but in loadnewRow function loop =" + randomScale);
for (var z:Number =0; z < clipsFitYTotal; z++) {
for (var b:Number = 0; b < clipsFitXTotal; b++) {
var ldr:Loader = new Loader();
var randomSelection = Math.floor( (Math.random()*filePaths.length) );
ldr.load(new URLRequest (filePaths[randomSelection]));
ldr.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
ldr.scaleX=ldr.scaleY=randomScale;//trace("randomScale in the loop is =" + randomScale);
ldr.y=finalRandomScaledHeight*z;//trace("finalRandomScaledHeight*z =" + ldr.y);
ldr.x=finalRandomScaledWidth*b;//trace("finalRandomScaledWidth*b =" + ldr.x);
ldr.alpha=.8;
//trace("[ldr] = number of children = " + ldr.numChildren);
//trace("[THIS] = number of children = " + this.numChildren);
//trace("[THIS] name is = " +this.name);
ldr.cacheAsBitmap = true;
//thumbholder.addChild(ldr);
addChildAt(ldr,1);
filePaths.splice(randomSelection, 1);
if (filePaths.length==0) {
filePaths.push("thmb1.swf","thmb2.swf","thmb3.swf","thmb4.swf","thmb5.swf",
"thmb6.swf","thmb7.swf","thmb8.swf", "thmb9.swf",
"thmb10.swf", "thmb11.swf");
}
}
}
}
I suggest you to use some library/framework to simplifiy things. As I use splinklibrary here is some exemplary code to solve your problem with the aid of splinklibrary:
public function Test() {
var filePaths : Array = ["thmb1.swf","thmb2.swf","thmb3.swf"];
var q : IQ = new Q();
q.register(QCompleteEvent.COMPLETE, onComplete);
for each (var path : String in filePaths) {
q.add(new QLoader(new URLRequest(path)));
}
q.start();
}
private function onComplete(e : QCompleteEvent) : void {
var displays : Array = new Array();
var q : IQ = IQ(e.getSource());
for each (var loader : QLoader in q.getItems()) {
displays.push(addChild(loader.getContent()));
}
arrange(displays);
show(displays);
}
private function arrange(displays : Array) : void {
// put in your code to arrange the loaded displayobjects
}
private function show(displays : Array) : void {
// put in your code to tween the loaded displayobjects
}
Of course you can reach your goal without a library too, but the point is that a library often helps to keep your code simple and concise.