AS3 | How to use var for name? - actionscript-3

My code is:
for (var i:int=1; i <= 12; i++) {
MovieClip(getChildByName('mouse' + '_' + i)).addEventListener(MouseEvent.CLICK, chooseTool);
}
function chooseTool(e:MouseEvent):void {
Mouse.hide();
var cursor:MovieClip = new "here is e.target.name"();
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
}
function follow(e:MouseEvent):void {
cursor.x=mouseX;
cursor.y=mouseY;
}
How to use "e.target.name" for name ?
Should become something like:
1) Click on button with name mouse_4
2) Function chooseTool create new MovieClip with name mouse_4();
example: var cursor:MovieClip = new mouse_4();

You can do it by using getDefinitionByName (Adobe documentation: getDefinitionByName)
for (var i:int=1; i <= 12; i++) {
MovieClip(getChildByName('mouse' + '_' + i)).addEventListener(MouseEvent.CLICK, chooseTool);
}
function chooseTool(e:MouseEvent):void {
Mouse.hide();
var o:Object = getDefinitionByName(e.target.name);
// ^ Gets a reference to the class object based on a string
if(o == null) throw new Error("Unable to find class " + e.target.name);
var c:Class = o as Class;
var cursor:MovieClip = new c() as MovieClip;
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
}
function follow(e:MouseEvent):void {
cursor.x=mouseX;
cursor.y=mouseY;
}
But this feels very ill structured way of doing it. Instead I would use a factory method (Factory pattern on Wikipedia)
for (var i:int=1; i <= 12; i++) {
MovieClip(getChildByName('mouse' + '_' + i)).addEventListener(MouseEvent.CLICK, chooseTool);
}
function chooseTool(e:MouseEvent):void {
Mouse.hide();
var cursor:MovieClip = getCursor(e.target);
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
}
function follow(e:MouseEvent):void {
cursor.x=mouseX;
cursor.y=mouseY;
}
function getCursor(target:MovieClip):MovieClip
{
switch(target.name)
{
case "cursor_4":
return new cursor_4();
default:
return new default_mouse();
}
}

You can't do it like that. That is not realy a name would be a new class/type. The solution is to extend the MovieClip and pass that name as a parameter . I do not know what you want to achieve but this is a good way to start.
class MyClip extends MovieClip
{
public var name:String="";
public function MyClip(name:String)
{
super();
this.name=name;
}
}
}
The code will look something like that and you can add all the logic related with this type inside this class.

You can't Instantiate like this new "e.target.name"();
But if you want to take the reference of target,you can try this.
var cursor:MovieClip =e.target as MovieClip;
you cannot modify the name property of cursor.

Related

Can I get some tips With My Inventory system for my game. I want to use an object in my inventory with another object (like a Key to a Door)

This is my Main Class:
package {
import flash.display.*;
public class InventoryDemo extends MovieClip {
var inventory:Inventory;
public function InventoryDemo() {
}
public function initialiseInventory():void
{
inventory = new Inventory(this);
inventory.makeInventoryItems([d1,d2]);
}
}
}
I used a sprite indicator to show that the items are inside the inventory.
And this is my child class:
package {
import flash.display.*;
import flash.events.*;
public class Inventory
{
var itemsInInventory:Array;
var inventorySprite:Sprite;
public function Inventory(parentMC:MovieClip)
{
itemsInInventory = new Array ;
inventorySprite = new Sprite ;
inventorySprite.x = 50;
inventorySprite.y = 360;
parentMC.addChild(inventorySprite);
}
function makeInventoryItems(arrayOfItems:Array)
{
for (var i:int = 0; i < arrayOfItems.length; i++)
{
arrayOfItems[i].addEventListener(MouseEvent.CLICK,getItem);
arrayOfItems[i].buttonMode = true;
}
}
function getItem(e:Event)
{
var item:MovieClip = MovieClip(e.currentTarget);
itemsInInventory.push(item);
inventorySprite.addChild(item);
item.x = itemsInInventory.length - 1 * 40;
item.y = 0;
item.removeEventListener(MouseEvent.CLICK,getItem);
item.addEventListener(MouseEvent.CLICK,useItem);
}
function useItem(e:Event)
{
var item:MovieClip = MovieClip(e.currentTarget);
trace(("Use Item:" + item.name));
}
}
}
Currently i can only click and trace the output, I was wondering how i can drag the sprite and use it to another object...like a key to unlock a door. Big thanks, btw im new in as3 and im trying to learn from stack overflow.
item.addEventListener(MouseEvent.CLICK,useItem);
var drag:Boolean;
function useItem(e:Event)
{
var item:MovieClip = MovieClip(e.currentTarget);
trace(("Use Item:" + item.name));
if(drag == false)
{
item.startDrag();
drag = true;
}else{
item.stopDrag();
drag = false;
findAction(e);
}
}
function findAction(e)
{
// Check the position of the key relative to the door.
}
Haven't really checked it but it'd probably work if you did something similar.

Moving object in AS3 while it is translated by Matrix3D

First, please look at my SWF: http://krakow45.pl/spec/warcaby/Warcaby3D.html
You can move pawns and it works pretty well. But the problem starts whem you translate game board (by pressing any of direction keys). After this you cant move pawns. Here is little piece of my code:
translation:
case Keyboard.LEFT:
_matrix = new Matrix3D();
_matrix.appendTranslation(0, -200, 0);
_matrix.appendRotation(_rot++, Vector3D.X_AXIS);
_matrix.appendTranslation(0, 200, _depth);
_table._board.transform.matrix3D = _matrix;
break;
moving the pawn:
private function MouseDown(event:MouseEvent):void
{
var pawn:Pawn = event.currentTarget as Pawn;
_xPos = pawn._xPos;
_yPos = pawn._yPos;
_txt.text = pawn._xPos + " - " + pawn._yPos + "\n";
pawn.startDrag();
}
Ok, I solved this by using something like this: (against startStag() )
private var _clicked:Boolean
private var _currentPawn:Pawn
private function MouseDown(event:MouseEvent):void
{
_clicked = true;
_currentPawn = event.currentTarget as Pawn;
// rest of my code
}
private function MouseMove(event:MouseEvent):void
{
if(_clicked)
{
_currentPawn.x = mouseX;
_currentPawn.y = mouseY;
}
}
private function MouseUp(event:MouseEvent):void
{
_clicked = false;
// rest of my code
}

how to detect all tween complete in starling-framework

I'm develop a game with starling-framework.my problem is how to detect all tween is complete when I add a set of elements into juggler.
current my plan is
while(some_condition){ //allocate a set of tween
var tween=createTween(tweenCount++);
tween.onComplete=function(){
tweenCount--;
}
}
function checkComplete(){
if(tweenCount==0)
doStuff();
else
setTimeout(checkComplete,1000);
}
and there is any better solution ? thanks for your time!
update
write a simple class to solve this ,seems like better
public class TweenMonitor
{
public function TweenMonitor()
{
throw new Error('static class');
}
private static var refDict:Dictionary = new Dictionary();
public static function monit(tweens:Vector.<Tween>,onAllFinish:Function,id:String='$default$'):void
{
for (var i:int = 0; i < tweens.length; i++)
{
var tween:Tween = tweens[i];
if (tween == null)
continue;
if (refDict[id] == null) {
refDict[id] = 1;
}else
refDict[id]++;
tween.addEventListener(Event.REMOVE_FROM_JUGGLER,function (e:Event):void
{
refDict[id]--;
if (refDict[id] == 0)
onAllFinish && onAllFinish();
});
}
}
}
You simply need the onComplete property of Tween Class (Starling Framework)
Here is an Example :
function addTween() {
var tween:Tween = new Tween(object, 2.0, Transitions.EASE_IN_OUT);
tween.animate("x", object.x + 50);
tween.animate("rotation", deg2rad(45));
tween.fadeTo(0);
tween.onComplete = tween_complete;
Starling.juggler.add(tween);
}
function tween_complete() {
// Handle Tween Complete Action Here
}
EDIT
In case of multiple tweens, you probably could do better by attaching a timer class instead of settimeout:
while(some_condition){ //allocate a set of tween
var tween=createTween(tweenCount++);
tween.onComplete = function() { tweenCount--; }
}
var timer : Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,update);
timer.start();
function update(e) {
if(tweenCount > 0) return;
timer.stop();
timer = null;
doStuff();
}
I would have done like this:
OnComplete = tweenDone;
...
function tweenDone() {
TweenCount--;
If(TweenCount == 0) doStuff();
}
Wrote this on a cell phone so I had to take a lot of shortcuts, if you need more info, leave a comment!

How to make new bubbles stack in my Bubble Shooter game?

I'm making a Bubble Shooter game, and I'm trying to make the bubble I'm fireing to stack and then be at the right place in the column. The bubbles I've placed on the board looks like this:
000000000000000
000000000000000
000000000000000
000000000000000
There's 4 rows with 15 bubbles. This is the code I have written so far:
Main
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.SpreadMethod;
public class Main extends Sprite {
private const ROT_SPEED:uint=2;
private const R:uint=18;
private const DEG_TO_RAD:Number=0.0174532925;
private const BUBBLE_SPEED:uint=10;
private var bubbleArr:Array=new Array();
private var loadArr:Array=new Array();
private var cannon:cannon_mc;
private var bubble:bubble_mc;
private var row:uint=0;
private var col:uint=0;
private var left:Boolean=false;
private var right:Boolean=false;
public var bubCont:Sprite;
private var loadCont:Sprite;
private var fire:Boolean=false;
private var vx,vy:Number;
public function Main() {
placeContainer();
placeCannon();
loadBubble();
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKUp);
addEventListener(Event.ENTER_FRAME,onEFrame);
trace("row= "+row+" , col= "+col);
}
private function placeCannon():void {
cannon=new cannon_mc();
addChild(cannon);
cannon.y=385.5;
cannon.x=320;
}
private function onKDown(e:KeyboardEvent):void {
switch(e.keyCode) {
case 37 :
left=true;
break;
case 39 :
right=true;
break;
case 38 :
if (! fire) {
fire=true;
var radians=(cannon.rotation-90)*DEG_TO_RAD;
vx=BUBBLE_SPEED*Math.cos(radians);
vy=BUBBLE_SPEED*Math.sin(radians);
}
break;
}
}
private function onKUp(e:KeyboardEvent):void {
switch(e.keyCode) {
case 37 :
left=false;
break;
case 39 :
right=false;
break;
}
}
private function onEFrame(e:Event):void {
if (left) {
cannon.rotation-=ROT_SPEED;
}
if (right) {
cannon.rotation+=ROT_SPEED;
}
if (fire) {
bubble.x+=vx;
bubble.y+=vy;
if (bubble.x<59) {
bubble.x=59;
vx*=-1;
}
if (bubble.x>(59+R*R)) {
bubble.x=59+R*R;
vx*=-1;
}
if (bubble.y<(40)) {
bubble.y=40;
}
}
}
public function placeContainer():void {
var iRow:Boolean=false;
bubCont=new Sprite();
addChild(bubCont);
for (var i:uint=0; i<4; i++) {
if (! iRow) {
for (var j:uint=0; j<15; j++) {
bubbleArr[i]=new Array();
bubbleArr[i][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i][j],i,j);
bubCont.addChild(bubble);
iRow=true;
row++;
col++;
}
} else {
for (j=0; j<15; j++) {
bubbleArr[i]=new Array();
bubbleArr[i][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i][j],i,j);
bubble.x=77+j*2*R;
bubCont.addChild(bubble);
iRow=false;
row++;
col++;
}
}
}
}
private function loadBubble():void {
addChild(bubble);
bubble.gotoAndStop(Math.floor(Math.random()*6))+1;
bubble.x=320;
bubble.y=410;
}
}
bubble_mc class:
package {
import flash.display.MovieClip;
public class bubble_mc extends MovieClip {
public function bubble_mc(val:uint,row:uint,col:uint) {
gotoAndStop(val+1);
name=row+"_"+col;
x=59+col*36;
y=40+row*32;
}
}
I have absolutley no idea how to make the bubbles stack together.. I have tried using hitTestObject-function and I have tried to write my own function that checks for collision and then calls a function that is supposed to place the bubble in the right place, but it doesn't work and I dont know why. I'm getting a error called TypeError: Error #1010.
Here is the collision function and the parkBubble function- which is supposed to place the bubbles in the right place:
private function parkBubble(bubble:bubble_mc,row:int,col:int):void {
var iRow:Boolean=false;
for (var j:uint=0; j<col; j++) {
trace("first for loop ");
for (var i:uint=row; i>0; i--) {
trace("second for loop ");
if (bubbleArr[i][j]!=null) {
trace("first if loop ");
if (! iRow) {
trace("second if loop ");
bubbleArr[i+1]=new Array();
bubbleArr[i+1][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i+1][j],(i+1),j);
bubCont.addChild(bubble);
iRow=true;
row++;
col++;
} else {
trace("first for loop after else ");
bubbleArr[i+1]=new Array();
bubbleArr[i+1][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i+1][j],(i+1),j);
bubble.x=77+j*2*R;
bubCont.addChild(bubble);
iRow=false;
row++;
col++;
}
}
}
}
removeChild(bubble);
fire=false;
loadBubble();
trace("slutet av parkBubble ");
}
private function collide(bub:bubble_mc):Boolean {
var dist_x:Number=bub.x-bubble.x;
var dist_y:Number=bub.y-bubble.y;
return Math.sqrt(dist_x*dist_x+dist_y*dist_y)<=2*R-4;
}
Was the TypeError on this line?
var placed_bubble:bubble_mc=new bubble_mc([row][col],row,col);
The [row] is an array, and [col] is an array. But the constructor expects an unsigned integer:
public function bubble_mc(val:uint,row:uint,col:uint) {
In order to copy the bubble to the bubble container, pass the frame number:
var placed_bubble:bubble_mc=new bubble_mc(bubble.currentFrame-1, row, col);
This might not be the only problem. TypeError often results from a variable not being defined, which could be from some other code that modifies the variable "bubble". For example, placeContainer assigns the bubbles in the container to the variable "bubble".
The function parkBubble always sets "iRow" to false, but if the bubble collides with a row above it that is odd you want iRow to be true.
var row:uint=Math.floor(bubble.y/(40+R*Math.sqrt(3)));
var iRow:Boolean= row % 2 == 1 ? true : false;
After it is at least compiling, you'll have less problems if you go back and simplify and optimize the math with some constant names. Then you'll more easily see the above code for calculating the row is not quite right. It should subtract the top margin (40). That is obvious with named constants:
private const Y_PER_ROW:int = int(R * Math.sqrt(3));
private const TOP:int = 40;
...
var row:uint = int((bubble.y - TOP) / Y_PER_ROW);
I would double-check your other calculations, too. Puzzle Bobble games usually set the odd rows to horizontally offset at radius, not at 2 radius (2 * R). So they fit together like hexagons.
The placeContainer function could be simplified. Most of the code in even or odd rows is the same, so could be taken out of the if block. And in this code you posted, I don't see why you need "row++" and "col++" in placeContainer. This is equivalent and easier to read:
for (var i:uint=0; i<4; i++) {
var xOffset:int = (i % 2) * 2 * R;
for (var j:uint=0; j<15; j++) {
bubbleArr[i] = new Array();
bubbleArr[i][j] = int(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i][j], i, j);
bubble.x += xOffset;
bubCont.addChild(bubble);
row++;
col++;
}
}
Then, the collision detection code could be simplified and optimized to avoid calculations when the bubble is far away and avoid the expensive square-root calculation:
Circle Collision Detection HTML5 Canvas
http://cgp.wikidot.com/circle-to-circle-collision-detection

Actionscript 3 compare two MovieClips

I dynamically add MovieClips to an DisplayObjectContainer. Some of these MovieClips loop through all children of DisplayObjectContainer to check gravitation and collision. Though, when I check if the current child is not equal to the caller MovieClip it seems to check the type only.
So basically, when I check MovieClip equality it seems to check the type only.
Main.as:
var planet:Planet = new Planet(holder);
planet.x = 0;
planet.y = 0;
planet.spawn();
var planet2:Planet = new Planet(holder);
planet2.x = 50;
planet2.y = 50;
planet2.spawn();
Planet.as:
public class Planet {
public var x:Number = 0;
public var y:Number = 0;
private var _holder:DisplayObjectContainer;
private var _mc:MovieClip;
public function Planet(holder:DisplayObjectContainer) {
_holder = holder;
_mc = new PlanetMovieClip();
_mc.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function spawn():void {
_holder.addChild(_mc);
}
private function enterFrameHandler(evt:Event):void {
for(var i:int = 0; i < _holder.numChildren; i++) {
var child:MovieClip = _holder.getChildAt(i) as MovieClip;
// the other planet never passes this check
if(child !== _mc) {
trace('child is not the same');
}
}
}
}
So am I doing something wrong, should I approach an other method or should I just add an property that generates a random token used for identification?
you should remove as MovieClip; in _holder.getChildAt(i);
I would change !== with !=
The rest of your code seems ok.
First of all you have error in the code you have given to create Planet movieclip.
It should be
var planet:Planet = new Planet(holder);
planet.x = 0;
planet.y = 0;
planet.spawn();
var planet2:Planet = new Planet(holder)
planet2.x = 50;
planet2.y = 50;
planet2.spawn();
Did you check for null in Planet.as ?
if(child!=null) {
if(child !== _mc)
trace('child is not the same');
else
trace("child same");
}
And ofcourse you can always assign some unique name to the movieclips, and use it for comparing.