Hello there,
I'm trying to create a little drag and drop game with some objects (bombs). The bombs are to walk in my screen on what number of ID is generated randomly. It then chooses a pink bomb or a blackbomb. Both bombs can be dragged(they must be dragged intro the right house), but while playing the game, more bombs of the same kind will appear , slightly more and faster.
Now the point which is my problem is that all the bombs of the same kind stop moving when I try to drag one of the bombs, which isn 't my intention.
I want the bombs to be dragged while the other bombs just keep appearing and I want the one draggable which the user wants to drag. So i want to give each bomb some sort of ID when dragging it.
Can anyone see what I'm missing?
Main class:
Actionscript Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
*
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
createBomb();
}
private function createBomb():void
{
var bomb:Bomb = new Bomb();
addChild(bomb);
}
}
}
Bomb class:
Actionscript Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* ...
*
*/
public class Bomb extends Sprite
{
//_rndBomb is for choosing a number between 0 and 1. Pink or black bomb. Chooses randomly.
private var _rndBomb:int;
private var _timerEasy:Timer;
private var _easyDelay:int = 2000;
private var _blackBombsHolder:Array = [];
private var _pinkBombsHolder:Array = [];
private var _randomMovement:int;
private var pinkBomb:PinkBomb;
public function Bomb():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_timerEasy = new Timer(_easyDelay, 0);
_timerEasy.addEventListener(TimerEvent.TIMER, makeBlackBombsWithTimer);
_timerEasy.start();
addEventListener(Event.ENTER_FRAME, moveBombs);
}
//create pink bomb class
private function createPinkBomb():void
{
pinkBomb = new PinkBomb();
addChild(pinkBomb);
pinkBomb.x = 275;
_pinkBombsHolder.push(pinkBomb);
pinkBomb.buttonMode = true;
}
private function dragPinkBomb(e:MouseEvent):void
{
e.currentTarget.velY = 0;
//pinkBomb.velX = mouseX;
e.currentTarget.startDrag();
trace(e.currentTarget.id);
}
private function stopDragPinkBomb(e:MouseEvent):void
{
e.currentTarget.stopDrag();
e.currentTarget.velY = 2;
}
private function createBlackBomb():void
{
var blackBomb:BlackBomb = new BlackBomb();
addChild(blackBomb);
blackBomb.x = 275;
_blackBombsHolder.push(blackBomb);
}
//create black bomb class
private function makeBlackBombsWithTimer(e:TimerEvent):void
{
_rndBomb = Math.ceil(Math.random() * 2);
makeRandomBomb(_rndBomb);
}
private function moveBombs(e:Event):void
{
for (var i:int = 0; i < _blackBombsHolder.length; i++)
{
_blackBombsHolder[i].y += 2;
}
for (var j:int = 0; j < _pinkBombsHolder.length; j++)
{
_pinkBombsHolder[j].y += pinkBomb.velY;
pinkBomb.id = j;
}
}
public function makeRandomBomb(id:int):void
{
if ( id == 1)
{
createBlackBomb();
trace("black");
}
if ( id == 2)
{
createPinkBomb();
pinkBomb.addEventListener(MouseEvent.MOUSE_DOWN, dragPinkBomb);
pinkBomb.addEventListener(MouseEvent.MOUSE_UP, stopDragPinkBomb);
trace("pink");
}
}
}
}
PinkBomb Class:
Actionscript Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
*
*/
public class BlackBomb extends Sprite
{
private var _id:int;
private var _xPos:int = Math.floor(Math.random() * 100) + 50;
public function BlackBomb():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
createBlackBomb();
}
private function createBlackBomb():void
{
var black:Sprite = new Sprite();
addChild(black);
black.graphics.beginFill(0x000000, 1);
black.graphics.drawCircle(0, 0, 20);
black.graphics.endFill();
black.x = _xPos;
}
public function get id():int
{
return _id;
}
public function set id(value:int):void
{
_id = value;
}
}
}
PinkBomb Class:
Actionscript Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
*
*/
public class PinkBomb extends Sprite
{
private var _id:int;
private var _xPos:int = Math.floor(Math.random() * 100) + 50;
private var _velY:int = 2;
private var _velX:int = 0;
public function PinkBomb():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
createPinkBomb();
}
private function createPinkBomb():void
{
var pink:Sprite = new Sprite();
addChild(pink);
pink.graphics.beginFill(0xFF0000, 1);
pink.graphics.drawCircle(0, 0, 20);
pink.graphics.endFill();
pink.x = _xPos
trace(_xPos);
}
public function get id():int
{
return _id;
}
public function set id(value:int):void
{
_id = value;
}
public function get velY():int
{
return _velY;
}
public function set velY(value:int):void
{
_velY = value;
}
public function get velX():int
{
return _velX;
}
public function set velX(value:int):void
{
_velX = value;
}
}
}