-1

I tried to ask this question here, but couldnot get a satisfactory answer. (Why should compiler allow super-class of a parameter in a function) Trying to ask again. Why casting, doesnot loose the member functions, when done on classes ? In the following, i expected, that after casting to Sprite, the class should loose all it's information regarding the current frame. But it retains the information, as if casting is just a "show-off", not "actually done" internally ?

import flash.display.MovieClip;
import flash.display.Sprite;

var mc:MovieClip 
mc.gotoAndStop(2);
trace(mc.currentFrame);  // output 2  --> that's ok 

var sprite:Sprite = Sprite(mc)

trace( MovieClip(sprite).currentFrame);//output 2, value not lost, which is questionable

Output: 2 2

I know, the answer can be, it's how Adobe did it. But what's the logic ? Ideal logic should be that, after casting, and recasting, all the values must be restored to default. ( '0' in this case )

V.

Community
  • 1
  • 1
Vishwas
  • 1,533
  • 2
  • 19
  • 40
  • From me flash trace "0" in both traces but this maybe is notmal for empty clip. pleas add var mc:MovieClip = new MovieClip(); – Azzy Elvul Mar 05 '13 at 14:45
  • oh.. , i had already added a movieclip on stage. Naturally, the movieclip shouldnot be empty.It should be having 2 frames atleast. Thnx – Vishwas Mar 05 '13 at 14:46

2 Answers2

2

Cast will not clear the member fields. Casting show what others can access from this object but not change the object.

In flash objects are passed by reference. Imagine you have a object in memory ( mc in your case ). When you create sprite by casting the mc you pass the reference ( memory address ) to sprite var. At this point mc and sprite points to same address in memory. When you cast sprite to MovieClip you pass same address from mc. And this address is address for MovieClip mc at this point currentFrame will access the value of mc. Casting is not like copy constructors

Edit. By using this link I create simple code that proove my words:

var memoryHash:String;

var mc: MovieClip = new MovieClip();
var s: Sprite = Sprite( mc );
try
{
      FakeClass(mc);
}
catch (e:Error)
{
    memoryHash = String(e).replace(/.*([@|\$].*?) to .*$/gi, '$1');
}

trace( memoryHash );
try
{
    FakeClass(s);
}
catch (e:Error)
{
        memoryHash = String(e).replace(/.*([@|\$].*?) to .*$/gi, '$1');
}

trace( memoryHash );

And the Fake class:

package { public class FakeClass { public function FakeClass() {} } }

The output will show the memory address of mc and s. As you can see thay are equal. In my mashine the output is

@35ed041 @35ed041

Community
  • 1
  • 1
Azzy Elvul
  • 1,403
  • 1
  • 12
  • 22
  • 2
    public members - field and methods. In general when you cast you dosn't change the object. In flash objects are passed by reference. Imagine you have a object in memory ( mc in your case ). When you create sprite by casting the mc you pass the reference ( memory address ) to sprite var. At this point mc and sprite points to same address in memory. When you cast sprite to MovieClip you pass same address from mc. And this address is address for MovieClip mc at this point currentFrame will access the value of mc. Casting is not like copy constructors – Azzy Elvul Mar 05 '13 at 14:53
  • Azzy Elvul says the right words. And another sample that will show you that casting doesn't change original object in any aspect: `import flash.display.*; var mc:MovieClip = new MovieClip(); var c:Sprite = Sprite(mc) trace (mc.currentFrame) //trace (c.currentFrame) // Error 1119: Access of possibly undefined property currentFrame through a reference with static type flash.display:Sprite. trace (c["currentFrame"])` – Smolniy Mar 05 '13 at 15:32
  • You have error because c is from type Sprite and it does not have method currentFrame. As I say in the begining Casting show what others can access from this object ( in simple words ). var c:Sprite = Sprite(mc); -> means Treat content of memory address (mc) as Sprite. – Azzy Elvul Mar 05 '13 at 15:39
0

I'm not sure what you're trying to do here, but things like MovieClip(sprite).currentFrame are obviously going to lead to weird results. Because you're casting your sprite as a movieclip, it will have a currentframe. Try doing currentFrame on a sprite and see what happens.

lostPixels
  • 1,303
  • 9
  • 23