vendredi 14 avril 2017

How can i hitTesObject a movieclip inside a movieclip AS3?

I want my zombie to collect the brains(g1b1,g1b2,g1b3,g1b4). These brains are inside my movieclip 'platform1' because i need it to have a scrolling background effect. Well my zombie character can walk however he can't collect the brains.

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.display.Sprite;

public class z extends MovieClip {

    private var score:int=0;
    private var zombieSpeed:int=7; //speed for zombie
    private var gravity:int=1;
    private var jumpSpeed:int=0;//current speed for the jump
    private var jumpSpeedLimit:int=15; //how quick the jump

    private var jump, left, right, down, mainJump:Boolean=false; //zombie is not jumping

    public function z() {

    }

    public function Game1() {
        stop();
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
        zombie.addEventListener(Event.ENTER_FRAME, moveCharacter1);
        gamePlay1();
    }

    private function keyPressed(e:KeyboardEvent) {
        if (e.keyCode==Keyboard.LEFT) {
            left=true;
            zombie.gotoAndPlay("left");
        }
        if (e.keyCode==Keyboard.RIGHT) {
            right=true;
            zombie.gotoAndPlay("right");
        }
        if (e.keyCode==Keyboard.SPACE || Keyboard.UP) {
            jump=true;
        }
        if (e.keyCode==Keyboard.DOWN) {
            down=true;
        }
    }

    private function keyReleased(e:KeyboardEvent) {
        if (e.keyCode==Keyboard.LEFT || Keyboard.D) {
            left=false;
            zombieSpeed-=5;
            zombie.gotoAndStop("idleLeft");
        }
        if (e.keyCode==Keyboard.RIGHT || Keyboard.A) {
            right=false;
            zombieSpeed+=5;
            zombie.gotoAndStop("idleRight");
        }
        if (e.keyCode==Keyboard.SPACE || Keyboard.UP || Keyboard.W) {
            jump=false;
        }
        if (e.keyCode==Keyboard.DOWN || Keyboard.S) {
            down=false;
        }
    }

    private function moveCharacter1(e:Event) {
        if (left) {
            if (platform1.x<1000 && zombie.x==870) {
                platform1.x+=5;
            }
            else {
                if (zombie.x>120) {
                    zombie.x-=zombieSpeed;
                    trace(platform1.x);
                }
            }
        }
        if (right) {
            if (platform1.x>0 && zombie.x==870) {
                platform1.x-=5;
            }
            else {
                if (zombie.x<880) {
                    zombie.x+=zombieSpeed;
                    trace(platform1.x)
                }
            }
        }
        if (jump) {
            goJump();
    }

    function goJump() {
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
            jumpSpeed *= 1 + jumpSpeedLimit/50;
        }
        zombie.y += jumpSpeed;      
        for (var i=0;i<5;i++) {
            if(platform1.hitTestPoint(zombie.x,zombie.y,true)) {
                zombie.y--;
                jumpSpeed=0;
                mainJump=false;
            }
        }
    }

    **private function gamePlay1() {
        if(zombie.hitTestObject(platform1.g1b1)){
            platform1.g1b1.visible = false;
            score+=10;
        }
        if(zombie.hitTestObject(this.platform1.g1b2)){
            platform1.g1b2.visible = false;
            score+=10;
        }
        if(zombie.hitTestObject(platform1.g1b3)){
            platform1.g1b3.visible = false;
            score+=10;
        }
        if(zombie.hitTestObject(this.platform1.g1b4)){
            platform1.g1b4.visible = false;
            score+=10;
        }       
   }**

}

Aucun commentaire:

Enregistrer un commentaire