-
help with attachMovie
need a little help with attaching movie clip
working on a little game where you plant bombs when pressing spacebar
here is the code
Code:
if (Key.isDown(Key.SPACE)) {
i++;
var newname = "bomb"+i;
attachMovie("bomb", "bomb"+i, this.getNextHighestDepth());
_root[newname]._y = _root.hero._y;
_root[newname]._x = _root.hero._x;
_root[newname].onEnterFrame = function() {
this works fine just one problem, the newly attached movieclip now follows the character movieclip... how can i stop this, but still have the bomb place in the characters position?
also is there away i can make it attack a limited amount of bombs
please help :(
thank you
-
1 Attachment(s)
i fixed this via using button rather than keyboard :) but now im stuck with the hitTest it isnt calling it :(
can anyone please help
i attached a sample of what im trying to do if that will help
-
Removed all codes from clips, placed code below in first frame main timeline
Removed variable from textfield and gave it an instance name of 'txt'.
Removed 'add bomb' button, added code for space bar.
Make sure when you test it, that you disable keyboard shortcuts.
Code:
var bomb = 5;
var armed = false;
var bombs = [];
var mc = null;
var keyListener = new Object();
hero.power = 4;
txt.text = bomb;
function makeBomb() {
if (bomb > 0) {
bomb -= 1;
txt.text = bomb;
mc = attachMovie("bomb", "bomb" + this.getNextHighestDepth(), this.getNextHighestDepth());
mc._y = hero._y;
mc._x = hero._x;
mc.damage = false;
bombs.push(mc);
} else if (bomb <= 0) {
trace("out of bombs");
}
}
keyListener.onKeyDown = function() {
if (Key.isDown(Key.SPACE) && armed == false) {
armed = true;
makeBomb();
}
};
keyListener.onKeyUp = function() {
if (Key.getCode() == Key.SPACE) {
armed = false;
}
};
function doUpDate() {
if (Key.isDown(Key.LEFT)) {
hero._x -= hero.power;
} else if (Key.isDown(Key.RIGHT)) {
hero._x += hero.power;
} else if (Key.isDown(Key.UP)) {
hero._y -= hero.power;
} else if (Key.isDown(Key.DOWN)) {
hero._y += hero.power;
}
for (var i in bombs) {
mc = bombs[i];
if (mc._currentframe == mc._totalframes) {
mc = bombs.splice(i, 1);
removeMovieClip(mc);
break;
} else if (mc._currentframe >= 14 && mc.damage == false) {
if (hero.hitTest(mc._x, mc._y, true)) {
mc.damage = true;
trace("hit");
}
}
}
}
Key.addListener(keyListener);
hero.onEnterFrame = doUpDate;
HTH
-
thanks a million, works just how i wanted it.. cheers!!! :thumbsup:
-
Using distance instead of hit test...
Code:
function doUpDate() {
if (Key.isDown(Key.LEFT)) {
hero._x -= hero.power;
} else if (Key.isDown(Key.RIGHT)) {
hero._x += hero.power;
} else if (Key.isDown(Key.UP)) {
hero._y -= hero.power;
} else if (Key.isDown(Key.DOWN)) {
hero._y += hero.power;
}
for (var i in bombs) {
mc = bombs[i];
if (mc._currentframe == mc._totalframes) {
mc = bombs.splice(i, 1);
removeMovieClip(mc);
break;
} else if (mc._currentframe >= 14 && mc.damage == false) {
if (getMDistance(hero, mc)< 60) {
mc.damage = true;
trace("hit");
}
}
}
}
function getMDistance(p1, p2):Number {
return Math.abs(p1._x - p2._x) + Math.abs(p1._y - p2._y);
}