Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 - Clicked object

Explorer ,
Jul 20, 2014 Jul 20, 2014

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);

}

TOPICS
ActionScript
396
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jul 21, 2014 Jul 21, 2014

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

...
Translate
LEGEND ,
Jul 20, 2014 Jul 20, 2014

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 20, 2014 Jul 20, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2014 Jul 21, 2014

I've managed it. I think it is OK.

var piece:MovieClip = MovieClip(event.currentTarget);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 21, 2014 Jul 21, 2014
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines