将另一个影片剪辑添加到另一个帧

时间:2016-06-14 06:00:33

标签: actionscript-3 flash

我正在尝试将新的影片剪辑添加到我的射击游戏的下一帧中。

我正在使用Actionscript 3.0

为我的评估提供我需要帮助的基础。当得分= 50时,切换到下一帧。这就是我想为用户添加一种新型影片剪辑的地方!

这是我到目前为止的代码。

FRAME 1

    //Tate's open screen
stop(); //makes the screen wait for events

paraBtn.addEventListener(MouseEvent.CLICK, playClicked); //this line is making your button an mouse click event

function playClicked(evt: MouseEvent): void { // now we are calling the event from this function and telling it to go to the next frame we labelled play
    gotoAndStop("frame2");
    // All rights of this music goes towards the makers of the game "Risk of Rain" which was made by Hapoo Games
}

FRAME 2(游戏实际开始的地方)

    stop();
// This plays the sound when left click is used
var spitSound: Sound = new Sound();
spitSound.load(new URLRequest("gunSound.mp3"));
//This plays the gameover sound when your lives reach 0
var overSound: Sound = new Sound();
overSound.load(new URLRequest("Gameover.mp3"));
//This plays the sound when you are hit
var etSound: Sound = new Sound();
etSound.load(new URLRequest("Urgh.mp3"));


//This sets the lives and points.
stage.addEventListener(MouseEvent.MOUSE_MOVE, aimTurret);
var points: Number = 0;
var lives: Number = 3;
var target: MovieClip;
var _health: uint = 100;
initialiseCursor();

//This variable stops the multiple errors with the move objects and bullets and range to stop compiling.
var Gameover: Boolean = false;

//This aims the turrent to where you mouse is on the screen
function aimTurret(evt: Event): void {
    if (Gameover == false) {
        gun.rotation = getAngle(gun.x, gun.y, mouseX, mouseY);
        var distance: Number = getDistance(gun.x, gun.y, mouseX, mouseY);

        var adjDistance: Number = distance / 12 - 7;
    }

}

function getAngle(x1: Number, y1: Number, x2: Number, y2: Number): Number {
    var radians: Number = Math.atan2(y2 - y1, x2 - x1);
    return rad2deg(radians);
}

function getDistance(x1: Number, y1: Number, x2: Number, y2: Number): Number {
    var dx: Number = x2 - x1;
    var dy: Number = y2 - y1;
    return Math.sqrt(dx * dx + dy * dy);
}

function rad2deg(rad: Number): Number {
    return rad * (180 / Math.PI);
}
//Starts lives and shows text next to the numbers
function initialiseCursor(): void {
    Mouse.hide();
    target = new Target();
    target.x = mouseX;
    target.y = mouseY;
    target.mouseEnabled = false;
    addChild(target);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, targetMove);
    stage.addEventListener(MouseEvent.MOUSE_DOWN, targetDown);
    stage.addEventListener(MouseEvent.MOUSE_UP, targetUp);
    livesdisplay.text = String(lives) + " Lives Left";
    pointsdisplay.text = String(points) + " Points";
}

function targetMove(evt: MouseEvent): void {
    target.x = this.mouseX;
    target.y = this.mouseY;
}

function targetDown(evt: MouseEvent): void {
    target.gotoAndStop(2);
}

function targetUp(evt: MouseEvent): void {
    target.gotoAndStop(1);
}
//Starts bullets and speed of bullets, also addding new arrays for baddies
var gunLength: uint = 90;
var bullets: Array = new Array();
var bulletSpeed: uint = 20;
var baddies: Array = new Array();
stage.addEventListener(MouseEvent.MOUSE_DOWN, fireGun);
function fireGun(evt: MouseEvent) {
    if (Gameover == false) {
        var bullet: Bullet = new Bullet();
        bullet.rotation = gun.rotation;
        bullet.x = gun.x + Math.cos(deg2rad(gun.rotation)) * gunLength;
        bullet.y = gun.y + Math.sin(deg2rad(gun.rotation)) * gunLength;
        addChild(bullet);
        bullets.push(bullet);
        spitSound.play();
    }
}
function deg2rad(deg: Number): Number {
    return deg * (Math.PI / 180);
}

stage.addEventListener(Event.ENTER_FRAME, moveObjects);

function moveObjects(evt: Event): void {
    if (Gameover == false) {
        moveBullets();
        moveBaddies();
    }
}

function moveBullets(): void {
    for (var i: int = 0; i < bullets.length; i++) {
        var dx = Math.cos(deg2rad(bullets[i].rotation)) * bulletSpeed;
        var dy = Math.sin(deg2rad(bullets[i].rotation)) * bulletSpeed;
        bullets[i].x += dx;
        bullets[i].y += dy;
        if (bullets[i].x < -bullets[i].width || bullets[i].x > stage.stageWidth + bullets[i].width || bullets[i].y < -bullets[i].width || bullets[i].y > stage.stageHeight + bullets[i].width) {
            removeChild(bullets[i]);
            bullets.splice(i, 1);
        }
    }
}
// This is the start of the timer
var timer: Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, addBaddie);
timer.start();

// Adding army men on a timer and only from the top side
function addBaddie(evt: TimerEvent): void {
    var baddie: Baddie = new Baddie();
    var side: Number = Math.ceil(Math.random() * 1);
    if (side == 1) {
        baddie.x = Math.random() * stage.stageWidth;
        baddie.y = -baddie.height;
    }
    baddie.angle = getAngle(baddie.x, baddie.y, gun.x, gun.y);
    baddie.speed = 7;
    addChild(baddie);
    baddies.push(baddie);
}

function moveBaddies(): void {
    for (var i: int = 0; i < baddies.length; i++) {
        var dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
        var dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
        baddies[i].x += dx;
        baddies[i].y += dy;
        if (baddies[i].hitTestPoint(gun.x, gun.y, true)) {
            removeChild(baddies[i]);
            baddies.splice(i, 1);
            loseLife();
            //If baddie was removed then we don’t check for bullet hits 
        } else {
            checkForHit(baddies[i]);
        }
    }
}

function checkForHit(baddie: Baddie): void {
    for (var i: int = 0; i < bullets.length; i++) {
        if (baddie.hitTestPoint(bullets[i].x, bullets[i].y, true)) {
            removeChild(baddie);
            points++;
            if (points == 50) {
                gotoAndStop("frame3")
            }
            pointsdisplay.text = String(points) + " Points";
            baddies.splice(baddies.indexOf(baddie), 1);
        }
    }
}


// Keeping track of lost lives and when hitting 0 doing to frame four, also displaying "Lose life!"
function loseLife(): void {
    etSound.play();
    lives--;
    if (lives == 0) {
        Gameover = true;
        overSound.play();
        gotoAndStop("frame4")
    }
    livesdisplay.text = String(lives) + " Lives Left";
    trace("Lose Life!");
}

FRAME 3(这是我需要帮助的地方,添加另一个影片剪辑)(我为名为“BaddieRed”的框架制作了一个,其中Linkage为“Baddiered”

    stop();
// The code from frame2 carries over and works the same in this frame

FRAME 4(这是游戏的屏幕)

    stop();
timer.stop();

// User need to close by pressing the close button 
//And restart the game manually

1 个答案:

答案 0 :(得分:0)

这是你想要实现的目标吗?有点像...

function addBaddie(evt: TimerEvent): void 
{
    var baddie : MovieClip;
    if (points < 50) { var bad1: Baddie = new Baddie(); baddie = bad1; }
    if (points >= 50) { var bad2 : Baddiered = new Baddiered(); baddie = bad2; }

    var side: Number = Math.ceil(Math.random() * 1);
    if (side == 1) 
    {
        baddie.x = Math.random() * stage.stageWidth;
        baddie.y -= baddie.height;
    }
    baddie.angle = getAngle(baddie.x, baddie.y, gun.x, gun.y);
    baddie.speed = 7;
    addChild(baddie);
    baddies.push(baddie);
}