Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-12-2012, 04:05 AM   #1
an1
Registered User
 
Join Date: Feb 2012
Posts: 26
Default Accessing variable from another class is changing its value?

Hi, hoping someone could help me out with this.. I have this variable count in one class to keep track of a movieclip being dragged and dropped on another movieclip. When dragged, the value of the variable becomes 1. When I test this within the class, the value is reflected as 1. But when I access the same variable from another class, the value is reflected as 0. I have on idea why this is happening and am unable to solve it:/

The class where the variable is created:
ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.MouseEvent;     import flash.geom.Point;     //import fl.controls.CheckBox;     //import fl.controls.RadioButton;         public class pine extends MovieClip     {         protected var originalPosition:Point;         public var count:Number=0;                 //constructor         public function pine()         {             //this.subClicked2=subClicked2;             originalPosition=new Point(x,y);                     buttonMode=true;             //event listener on mouse click             addEventListener(MouseEvent.MOUSE_DOWN, down);             //counting();                      }                         //handle mouse click event         public /*protected*/ function down(event:MouseEvent):void         {             //allows object to come to front when moved             parent.addChild(this);             startDrag();             //event listener on mouse release             stage.addEventListener(MouseEvent.MOUSE_UP, stageUp);         }                 public /*protected*/ function stageUp(event:MouseEvent):void         {             //handle mouse release event             stage.removeEventListener(MouseEvent.MOUSE_UP, stageUp);             stopDrag();             if (dropTarget) {                 if (x>275 && x<847 && y>0 && y<543)                 {                         //scale up                         scaleX=scaleY=1.5;                         x=530;                         y=220;                         //make transparent                         alpha=0.5;                         count+=1;                         //no more mouseover                         buttonMode=false;                         stage.removeEventListener(MouseEvent.MOUSE_DOWN, down);                         counting();                     }                                     //else if dropped on another object, return to original position                     else {                         returnToOriginalPosition();                     }                 }                 else  {                     returnToOriginalPosition();                 }                         }                 //bring object back to its original position         public /*protected*/ function returnToOriginalPosition():void         {             x=originalPosition.x;             y=originalPosition.y;         }                 public function counting():void         {             trace("pineapples="+count);         }     } }

This is the class which is accessing the variable count:
ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.Event;     import flash.events.MouseEvent;     public class pizza extends MovieClip     {         public var totalIngs:Number;         var p:pine=new pine();         // var ol:olives=new olives();         //public var countPine:Number;         //public var countOlives:Number;         //public var totalIngs:Number;         public function pizza()         {             this.totalIngs = totalIngs;             this.countPine = countPine;             //this.countOlives = countOlives;             //trace("entered constructor");                     }                 public function total():void         {             //trace("entered total function");             countPine=p.count;             trace("total function: pineapples present = "+countPine);             //trace("working?");         }     } }

Please advise on how this can be fixed, thanks!
an1 is offline   Reply With Quote
Old 05-12-2012, 09:28 AM   #2
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

you'r doing some things i don't quite understand. can you explain the purpose of these lines:

Code:
parent.addChild(this);
This seems like a circular reference. The object is trying to add itself to where it already is ?

Code:
this.totalIngs = totalIngs;
this.countPine = countPine;
and this too. you seem to be setting two variables to.. themselves ?


Apart from anything, i don't see countPine being declared anywhere. You're trying to set it then read the value of it. Try, in the same line, also tracing the value of p.count. is that 0 too ?
WarKirby is offline   Reply With Quote
Old 05-12-2012, 09:35 AM   #3
an1
Registered User
 
Join Date: Feb 2012
Posts: 26
Default

Code:
parent.addChild(this);
I've done this to make sure the movie clip being dragged is visible and doesn't go under the movieclip it is being dropped on. I found this tutorial online and this was the method suggested to do that..

Code:
this.totalIngs = totalIngs;
this.countPine = countPine;
Over here, I was just trying to intialize the variables in the constuctor. I guess this is redundant..

I think I might have made a mistake while copying and pasting the code over, but countPine has been declared in the code. p.count also reflects a value of 0. I just don't understand why:/
an1 is offline   Reply With Quote
Old 05-12-2012, 09:57 AM   #4
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

Quote:
Originally Posted by an1 View Post
Code:
parent.addChild(this);
I've done this to make sure the movie clip being dragged is visible and doesn't go under the movieclip it is being dropped on. I found this tutorial online and this was the method suggested to do that..
ahh i see. it sounds like a rather hackish way. This is what setChildIndex is made for. try

parent.setChildIndex(this, parent.numChildren - 1);



Quote:
I think I might have made a mistake while copying and pasting the code over, but countPine has been declared in the code. p.count also reflects a value of 0. I just don't understand why:/
is it possible you have multiple instances of the pineapple then? i think that's most likely to be the problem.

try tracing p.name, and also have the pineapple trace its own name. It willl read something like instance82, but you can compare the numbers and see if they're actually the same object or not.
WarKirby is offline   Reply With Quote
Old 05-12-2012, 10:08 AM   #5
an1
Registered User
 
Join Date: Feb 2012
Posts: 26
Default

Thanks for the advice! I don't think I have multiple instances of the pineapple, but I traced p.name and got instance46. How do I get pineapple to trace its own name?
an1 is offline   Reply With Quote
Old 05-12-2012, 11:21 AM   #6
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

Quote:
Originally Posted by an1 View Post
How do I get pineapple to trace its own name?
o.o ?

i though that was pretty obvious

trace(name);
or even trace(this.name);

if you know to trace p.name, isn't that logical ?


Also, while you're tracing p.count, try also running p.counting(); so that the pineapple traces its own count value. see if the results are different.
WarKirby is offline   Reply With Quote
Old 05-12-2012, 12:24 PM   #7
an1
Registered User
 
Join Date: Feb 2012
Posts: 26
Default

Oh, thanks it just didn't strike I guess.. I did that. p.counting() returned a value of 0 and when I trace pine itself I get instance9.

How should I go about resolving this though?
an1 is offline   Reply With Quote
Old 05-12-2012, 04:59 PM   #8
an1
Registered User
 
Join Date: Feb 2012
Posts: 26
Unhappy

Someone help out please! I can't find a solution to this:/
an1 is offline   Reply With Quote
Old 05-12-2012, 10:44 PM   #9
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

at this point i think it would be most helpful to upload your project for inspection. i doubt we can reproduce the issue from these two code segments alone.
WarKirby is offline   Reply With Quote
Old 05-13-2012, 09:52 AM   #10
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

This code I provide is not 100% correct but you can have a look into it.
ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.Event;     import flash.events.MouseEvent;     import flash.utils.getDefinitionByName;     import flash.utils.getQualifiedClassName;     public class Pizza extends MovieClip {         public var totalIngs:Number;         var p:Pineapple;//=new Pineapple();         var o:Olive;//=new Olive();         public var countPine:Number;         private var fruitArray:Array=new Array({vNum:"p",cNum:Pineapple},{vNum:"o",cNum:Olive});         public function Pizza() {             this.totalIngs = totalIngs;             this.countPine = countPine;             addEventListener(MouseEvent.CLICK,clicked);         }         public function addFruit(fruit) {             var fruits;             for (var i=0; i<fruitArray.length; i++) {                 fruits=getQualifiedClassName(fruitArray[i].cNum);                 var theFruit=getQualifiedClassName(fruit);                 trace(fruits);                 if (fruits==theFruit) {                     this[fruitArray[i].vNum]=fruit;                     trace("fruits :"+fruitArray[i].vNum);                     trace(p);                 }             }         }         private function clicked(evt:MouseEvent):void {             total();         }         public function total():void {             for (var i=0; i<fruitArray.length; i++) {                 if (this[fruitArray[i].vNum]!=null) {                     this[fruitArray[i].vNum].counting();                 }             }         }     } }

ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.Event;     import flash.events.MouseEvent;     import flash.geom.Point;     public class Pineapple extends MovieClip {         protected var originalPosition:Point;         public var count:Number=0;         public function Pineapple() {             addEventListener(Event.ADDED_TO_STAGE,added);         }         private function added(event:Event):void {             buttonMode=true;             addEventListener(MouseEvent.MOUSE_DOWN, down);         }         public function down(event:MouseEvent):void {             originalPosition=new Point(x,y);                        startDrag();             addEventListener(MouseEvent.MOUSE_UP, stageUp);         }         public function stageUp(event:MouseEvent):void {             if (dropTarget) {                 trace(event.target.dropTarget.parent.name);                 if (dropTarget.parent.name=="pizza") {                     count+=1;                     counting();                     removeEventListener(MouseEvent.MOUSE_DOWN, down);                     removeEventListener(MouseEvent.MOUSE_UP, stageUp);                     stopDrag();                 } else {                     returnToOriginalPosition();                 }             } else {                 returnToOriginalPosition();             }         }         public function returnToOriginalPosition():void {             x=originalPosition.x;             y=originalPosition.y;             stopDrag()             removeEventListener(MouseEvent.MOUSE_UP, stageUp);              }         public function counting():void {             trace("pineapples="+count);         }     } }

ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.Event;     import flash.events.MouseEvent;     import flash.geom.Point;     import flash.utils.getQualifiedClassName;     public class Olive extends MovieClip {         protected var originalPosition:Point;         public var count:Number=0;         public function Olive() {             addEventListener(Event.ADDED_TO_STAGE,added);         }         private function added(event:Event):void {             buttonMode=true;             addEventListener(MouseEvent.MOUSE_DOWN, down);         }         public function down(event:MouseEvent):void {             originalPosition=new Point(x,y);             startDrag();             addEventListener(MouseEvent.MOUSE_UP, stageUp);         }         public function stageUp(event:MouseEvent):void {             if (dropTarget) {                 trace(event.target.dropTarget.parent.name);                 if (dropTarget.parent.name=="pizza") {                     count+=1;                     counting();                     removeEventListener(MouseEvent.MOUSE_DOWN, down);                     removeEventListener(MouseEvent.MOUSE_UP, stageUp);                     stopDrag();                 } else {                     returnToOriginalPosition();                 }             } else {                 returnToOriginalPosition();             }             stopDrag();         }         public function returnToOriginalPosition():void {             x=originalPosition.x;             y=originalPosition.y;             removeEventListener(MouseEvent.MOUSE_UP, stageUp);         }         public function counting():void {             trace("Olives="+count);         }     } }

Usage:

ActionScript Code:
var pine:Pineapple=new Pineapple(); var oliv:Olive=new Olive(); var pizz:Pizza=new Pizza(); pizz.name="pizza"; addChild(pizz); addChild(pine); addChild(oliv); pizz.addFruit(pine); pizz.addFruit(oliv); pine.x=10; pine.y=10; oliv.x=80; oliv.y=10; pizz.x=200; pizz.y=100;


__________________
Regards

arkitx
arkitx is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:44 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.