Copy link to clipboard
Copied
I have this function to create a new element to a bidimensional array.
function addPiece (row, col, newPiece, elem)
{
newPiece.row = row;
newPiece.col = col;
newPiece.name = arr[elem];
grid[row][col] = newPiece;
newPiece.addEventListener(MouseEvent.CLICK,clickPiece);
return newPiece;
}
elem is picking randomally from an array of movieclips.
elem = Math.ceil(Math.random()*max) - 1;
var type:* = new arr[elem] as arr[elem];
gameSprite.addChild(addPiece(row, col, type, elem));
I want in this function to create a variable of the same type as the clicked movieclip but I can't figure out how to do that:
something like:
function clickPiece(event:MouseEvent)
{;
var piece:Piece = Piece(event.currentTarget);
}
Variable types are set at compile time, not at runtime. So you can't create a variable on one run to be this type and another run to be a different type.
This is what Adobe ActionScript 3.0 * Interfaces are for. An Interface defines what properties and methods are going to be available without dictating a specific implementation. Once you have your Interface defined and your specific Classes implement it, then you can use Vectors instead of Arrays. This means that you don't have to worry about a
...Copy link to clipboard
Copied
I can't say I follow the various reinventions of things, but from what your last function appears to be trying to do, the answer might lie in something like the following...
var ClassRef:Class = Class(getDefinitionByName(evt.currentTarget.name));
var piece:* = new ClassRef();
Copy link to clipboard
Copied
It doesn't seem to be working.
In my function clickPiece I'm trying to get the object that is clicked to create a new "piece" where to keep the data of the clicked object so, when the second click is made, I could be able to make the swap between that 2nd object and 1st clicked object.
var piece:Piece = Piece(event.currentTarget);
Piece represents a movieclip from library and added dinamically. The Piece can be brown, red, pink, etc.
Copy link to clipboard
Copied
I've managed it. I think it is OK.
var piece:MovieClip = MovieClip(event.currentTarget);
Copy link to clipboard
Copied
Variable types are set at compile time, not at runtime. So you can't create a variable on one run to be this type and another run to be a different type.
This is what Adobe ActionScript 3.0 * Interfaces are for. An Interface defines what properties and methods are going to be available without dictating a specific implementation. Once you have your Interface defined and your specific Classes implement it, then you can use Vectors instead of Arrays. This means that you don't have to worry about a cast of an object at a specific index in the Vector failing--you'll always have an object in there that's the correct type. The outer Vector can be of type Vector.<YourInterface>.
Note that if the Display Objects you're storing in your array don't already share at least some functionality, you probably don't need to be treating them as a collection--or you can just treat them as a Vector of Display Objects, Sprites, or Movie Clips. It sounds like you basically wound up doing something like this in the end.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now