A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: help with attachMovie

Hybrid View

  1. #1

    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
    The one that smiles when things go wrong, Has thought of someone to blame it on

  2. #2
    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
    Attached Files Attached Files
    The one that smiles when things go wrong, Has thought of someone to blame it on

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    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

  4. #4
    thanks a million, works just how i wanted it.. cheers!!!
    The one that smiles when things go wrong, Has thought of someone to blame it on

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,518
    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);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center