Jump to content
Search Community

need help with object oriented syntax

jajohns8 test
Moderator Tag

Recommended Posts

FIrst off, just wanted to say thanks to the developer(s). This is an awesome set of classes.

 

I am a beginner to the world of AS3, and am just getting my feet wet with Object Oriented Programming, so please forgive some of the newb mistakes.

 

I KNOW this has to be a simple mistake involving my NEWBness to object oriented programming.

 

I have been able to implement TweenLite just fine in my mouseover event (a function in a Class file called Rooms.as) without any trouble.

 

My code, inside my mouseOverListener function, that gets the movie clip that was just rolled over and implements my TweenLite effect (increase scale) looks like this:

 

myMC=MovieClip(evt.target);
var myTween:TweenLite = new TweenLite(myMC, .5, {scaleX:3,scaleY:3});

 

That works great. Now, I have been trying to reverse this tween (myTween) in my mouse out listener function.

 

Inside my mouseoutLIstener function I have:

myTween.reverse();

 

When I try to test, I get the error:"1120:Access of undefined property myTween".

 

So this function is not seeing that TweenLite object (called myTween) I created.

 

I tried the following in my mouseoutlistener function, and it worked to bring the scale back to 1:

var myTween:TweenLite = new TweenLite(myMC, 1, {scaleX:1,scaleY:1});//works, but creates new object

 

The problem is, I don't want to create a new object. I want to reverse the one that was just created.

I have some more complex tweens that I would like to just use the reverse() function on.

 

I am pretty sure this comes down to me not understanding scope in OOP, but if I could get a handle on why this isnt working, it would most definitely HELP my understanding. :D

 

Any help would be greatly appreciated.

 

In case it might help, here is the entire Rooms.as class:

 

package
{

import flash.display.*;
import flash.events.*;
import com.abrahamyan.liquid.ToolTip;
import com.greensock.*;

  public class Rooms extends MovieClip
  {
    //  var speed:int; // how fast the fish is moving
      private var tooltip:ToolTip=ToolTip.getInstance()
      var myMC:MovieClip;
     // var myTween:TweenLite = new TweenLite(myMC, .5, {scaleX:3,scaleY:3});


       public function Rooms( ) // constructor
       {
        addEventListener(MouseEvent.MOUSE_OVER, mouseOverListener);
        addEventListener(MouseEvent.MOUSE_OUT, mouseOutListener);
        addEventListener(MouseEvent.CLICK, clickListener);
         //addEventListener(Event.ENTER_FRAME,move);

} // end constructor


function mouseOverListener(evt:MouseEvent):void {

gotoAndStop("over");
var over_name=evt.target.name;

evt.target.alpha=.3;

var act_type:String="over";
var icon_type:String="plant";
//ParseempData(xmlData,over_name,act_type,icon_type);
   trace("mouseOverListener method Rooms class");
   tooltip.followMouse=true;
tooltip.fixedWidth=150;
tooltip.multiLine=true;

  tooltip.show("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sodales placerat ligula. Proin elementum, ante vel consectetur pharetra");

myMC=MovieClip(evt.target);
//TweenLite.to(myMC,1, {scaleX:2,scaleY:2});
//var myTween:TweenLite = new TweenLite(myMC, .5, {scaleX:3,scaleY:3});
var myTween:TweenLite = new TweenLite(myMC, .5, {scaleX:3,scaleY:3});


}

function clickListener(evt:MouseEvent):void {
//setBlank();
gotoAndStop("over");
var over_name=evt.target.name;
var act_type:String="click";
var icon_type:String="plant";
//ParseempData(xmlData,over_name,act_type,icon_type);
   trace("clickListener method Rooms class");

}

function mouseOutListener(evt:MouseEvent):void {

gotoAndStop("out");

evt.target.alpha=1;
tooltip.hide();
trace("mouseOutListener method Rooms class");
var myTween:TweenLite = new TweenLite(myMC, 1, {scaleX:1,scaleY:1});//works, but creates new object
//myTween.reverse();//doesnt work, cant find myTween
}

   } // end class

} // end package

Link to comment
Share on other sites

The problem is that you're defining your myTween variable locally in your function. Local variables get removed from memory when the function finishes, but you're trying to reference myTween in another function. Think of it this way: local variables only exist inside the function where they're defined. You can easily resolve this problem by declaring the myTween variable at the class level instead of inside your function. Kinda like:

 

public class myClass {
   private var _tween:TweenLite;

   private function rollOverHandler(event:MouseEvent):void {
       _tween = new TweenLite(event.target, 0.5, {scaleX:3, scaleY:3});
   }

   private function rollOutHandler(event:MouseEvent):void {
       _tween.reverse();
   }

}

 

I'd also encourage you to use ROLL_OVER and ROLL_OUT instead of MOUSE_OVER/MOUSE_OUT. Check the docs for details, but it's a common mistake that can cause odd behavior (has nothing to do with TweenLite/Max).

Link to comment
Share on other sites

Aha. I see now. That worked. Thank you.

 

However, I have some sort of conflict that is causing the reverse function to not carry out completely in some circumstances.

 

I have 3 rectangles (right next to each other) on stage with a baseclass = Rooms, which is what my Rooms class is for:

package
{

import flash.display.*;
import flash.events.*;
import com.abrahamyan.liquid.ToolTip;
import com.greensock.*;

  public class Rooms extends MovieClip
  {

      private var tooltip:ToolTip=ToolTip.getInstance()
      var myMC:MovieClip;
     private var _tween:TweenLite;
     private var _mottween:TweenLite;




       public function Rooms( ) // constructor
       {
        addEventListener(MouseEvent.ROLL_OVER, mouseOverListener);
        addEventListener(MouseEvent.ROLL_OUT, mouseOutListener);
        addEventListener(MouseEvent.CLICK, clickListener);
         //addEventListener(Event.ENTER_FRAME,move);

} // end constructor


function mouseOverListener(evt:MouseEvent):void {

gotoAndStop("over");
var over_name=evt.target.name;

evt.target.alpha=.3;

var act_type:String="over";
var icon_type:String="plant";
//ParseempData(xmlData,over_name,act_type,icon_type);
   trace("mouseOverListener method Rooms class");
   tooltip.followMouse=true;
tooltip.fixedWidth=150;
tooltip.multiLine=true;

  tooltip.show("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sodales placerat ligula. Proin elementum, ante vel consectetur pharetra");

myMC=MovieClip(evt.target);

_tween = new TweenLite(evt.target, 0.25, {scaleX:1.5, scaleY:1.5});




}

function clickListener(evt:MouseEvent):void {
//setBlank();
gotoAndStop("over");
var over_name=evt.target.name;
var act_type:String="click";
var icon_type:String="plant";
   trace("clickListener method Rooms class");
   var newVal:Number;
   newVal=-600;
_mottween = new TweenLite(evt.target, 3, {x:newVal});

}

function mouseOutListener(evt:MouseEvent):void {

//gotoAndStop("out");

evt.target.alpha=1;
tooltip.hide();
trace("mouseOutListener method Rooms class");

_tween.reverse();

}

   } // end class

} // end package

 

If I rollover each rectangle several times, at various speeds, sometimes, one or more of the rectangles appears to not finish it's reverse myTween function(in the mouseOutListener function) so it stays large.

 

I have looked into adding the overwrite manager parameter, but I have not been able to get it to work correctly.

I have tried overwrite 0-5 and the issue persists.

 

 _tween = new TweenLite(evt.target, 0.25, {scaleX:1.5, scaleY:1.5, overwrite:4});

Link to comment
Share on other sites

It's a logic problem. Remember, when you create a normal tween, you define the end values and it records the CURRENT values as the starting values (well, whatever the values are at the time the tween is rendered for the first time). So in your example, let's say you roll over the object and it starts scaling to 1.5 and then when it's halfway there, you roll out. No problem, it reverses the tween and starts going back to 1 (where it started). But then let's say you rollover it again immediately. The new tween that you're creating will record the current value as the starting value (1.25 for example) and tween to 1.5. It will look fine until you roll out and reverse the tween at which point it will go backwards to the starting value and stop. Remember, that starting value was 1.25, not 1! So it will look like the tween stopped short when it actually didn't.

 

The solution is to either create your tween ahead of time and play()/reverse() it on rollover/rollout OR make sure you reset the values before creating your tween so that the starting values are always what you expect. Or just create a new tween each time in each direction (to 1.5 on rollover, and to 1 on rollout). Frankly, I think option #1 is the cleanest. Option 3 is 2nd cleanest.

 

Hope that helps.

Link to comment
Share on other sites

Greensock, thanks for your help, but I am a little lost. Your explanation makes sense to me as far as the tweening on the "current" value being the problem.

 

When you say "create it ahead of time" how would I do that?

Could you give me an example of how I would do that?

 

I tried placing "_mytween = new TweenLite(myMC, 0.25, {scaleX:1.5, scaleY:1.5});" in my constructor, but it doesnt know the myMC value so it gives me an error. Where else could I place it to create it ahead of time?

 

Thanks for taking the time to help.

 

my Rooms.as class file again:

 

package
{

import flash.display.*;
import flash.events.*;
import com.abrahamyan.liquid.ToolTip;
import com.greensock.*;
import com.greensock.easing.*;

  public class Rooms extends MovieClip
  {

      private var tooltip:ToolTip=ToolTip.getInstance()
      var myMC:MovieClip;
      var priorMC:MovieClip;
     private var _mottween:TweenLite;
     private var _revtween:TweenLite;
     private var _mytween:TweenLite;




       public function Rooms( ) // constructor
       {
        addEventListener(MouseEvent.ROLL_OVER, mouseOverListener);
        addEventListener(MouseEvent.ROLL_OUT, mouseOutListener);
        addEventListener(MouseEvent.CLICK, clickListener);
        OverwriteManager.init()
         //addEventListener(Event.ENTER_FRAME,move);

} // end constructor


function mouseOverListener(evt:MouseEvent):void {

gotoAndStop("over");
var over_name=evt.target.name;

evt.target.alpha=.3;

var act_type:String="over";
var icon_type:String="plant";
//ParseempData(xmlData,over_name,act_type,icon_type);
   //trace("mouseOverListener method Rooms class");
   tooltip.followMouse=true;
tooltip.fixedWidth=150;
tooltip.multiLine=true;

  tooltip.show("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sodales placerat ligula. Proin elementum, ante vel consectetur pharetra");

myMC=MovieClip(evt.target);

//_tween = new TweenLite(evt.target, 0.25, {scaleX:1.5, scaleY:1.5, overwrite:3});
  _mytween = new TweenLite(myMC, 0.25, {scaleX:1.5, scaleY:1.5});
_mytween.play();



}

function clickListener(evt:MouseEvent):void {
//setBlank();


   myMC=MovieClip(evt.target);
   trace(myMC);
gotoAndStop("over");
var over_name=evt.target.name;
var act_type:String="click";
var icon_type:String="plant";
   trace("clickListener method Rooms class");
   var newValx:Number;
   var newValy:Number;
   newValx=60;
   newValy=95
_mottween = new TweenLite(myMC, 1, {x:newValx,y:newValy,  ease:Back.easeIn});
   //trace(priorMC);

priorMC=myMC;
trace(priorMC);
}

function mouseOutListener(evt:MouseEvent):void {

//gotoAndStop("out");

evt.target.alpha=1;
tooltip.hide();
//trace("mouseOutListener method Rooms class");

_mytween.reverse();

}

   } // end class

} // end package

Link to comment
Share on other sites

I'd create it ahead of time if there's a single target you're talking about, but if you need to accommodate multiple different targets (using the same handler), then I'd just create a new tween each time and don't reverse() at all. Kinda like:

function rollOverHandler(event:MouseEvent):void {
   TweenLite.to(event.target, 0.5, {scaleX:1.5, scaleY:1.5});
}

function rollOutHandler(event:MouseEvent):void {
   TweenLite.to(event.target, 0.5, {scaleX:1, scaleY:1});
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...