简体   繁体   English

如何在“动作”面板中编码要生成到舞台上的对象?

[英]How can I code an object to spawn onto the stage in the Actions panel?

I am trying to introduce a new ball into my pong game, sort of like a power up. 我正在尝试在乒乓球比赛中引入一个新球,有点像加电。 I am writing all of my code in the Actions panel in the first frame. 我在第一帧的“动作”面板中编写所有代码。 The new ball should appear on the stage and start moving around randomly like the original ball. 新球应出现在舞台上,并像原始球一样开始随机移动。 Although I am using a code snippet and not .as file. 虽然我使用的是代码段,而不是.as文件。 So all of my code is in the Actions panel(Accessed by pressing f9). 因此,我所有的代码都在“动作”面板中(按f9进行访问)。

I would also like my dynamic text box to merge with the stage colour so that you can't see the white background. 我还希望将动态文本框与舞台颜色合并,以使您看不到白色背景。

I can't show you what the fla looks like because I have less than 10 reputation, but the dynamic text box will not merge into the background and instead has a white surrounding. 我无法显示fla的外观,因为我的声誉不到10,但是动态文本框不会合并到背景中,而是具有白色的周围环境。 This hides the ball when the ball goes up. 当球上升时,这会将球隐藏起来。

import flash.events.Event;
import flash.ui.Mouse;

//hide mouse
Mouse.hide();






init(); //initialises everything

var bSpeedX:int = -3.5;
var bSpeedY:int = -2.5;


// assign a maximum speed to the AI 
var compPaddleSpeed:int = 3.5; 

var pScore:int = 0;
var cScore:int = 0;




// Updates the score
function scoreUpdate():void {
    playerScore.text = ("Player Score: " + pScore);
    computerScore.text = ("AI Score: " + cScore);


}




function init():void //tells flash not to return values
{ 

    stage.addEventListener(Event.ENTER_FRAME, loop);

}   
/*we want the ySpeed to be larger if there 
is a greater difference between the y 
positions of the ball and paddle, so I started with 
(gameBallY-padY). To convert this difference 
into a number between -1 and 1, I divided 
this number by 25, which 
is half the height of the paddle. Finally, I wanted 
the ySpeed to be more powerful than 
just -1 to 1, and after a bit of trial and error 
I decided to times by 5 at the end 
to change the total magnitude of the new ySpeed.*/



//defying the laws of Physics
function calculategameBallAngle(padY:Number, gameBallY:Number):Number
{
    var ySpeed:Number = 5 * ((gameBallY-padY) / 25 );
    return ySpeed;
}





//main loop 
function loop(e:Event):void

{   
    //makes the paddle track the mouse
    playerPaddle.y = mouseY;

    //paddle AI
    if(compPaddle.y < gameBall.y - 10){ 
        compPaddle.y += compPaddleSpeed;//make it go up
    } else if(compPaddle.y > gameBall.y + 10){ 
        compPaddle.y -= compPaddleSpeed;//make it go down
    }

    //Collisions
    if( playerPaddle.hitTestObject(gameBall) == true ){
    if(bSpeedX < 0){
        bSpeedX *= -1;
        bSpeedY = calculategameBallAngle(playerPaddle.y, gameBall.y);
    }

} else if(compPaddle.hitTestObject(gameBall) == true ){ 
    if(bSpeedX > 0){
        bSpeedX *= -1;
        bSpeedY = calculategameBallAngle(compPaddle.y, gameBall.y);
    }

}


    //makes the gameBall move 
    gameBall.x += bSpeedX; //each frame, we add the bSpeedX to the ball's x position.
    gameBall.y += bSpeedY; //same for the bSpeedY to the ball's y postion.



    // checks to see if the ball misses the paddle 
    if(gameBall.x <= gameBall.width/2){
       gameBall.x = gameBall.width/2;
       bSpeedX *= -1;
       cScore ++;
       scoreUpdate();


    //keeps the ball within the stage
    } else if(gameBall.x >= stage.stageWidth-gameBall.width/2){
        gameBall.x = stage.stageWidth-gameBall.width/2;
        bSpeedX *= -1;
        pScore ++;
        scoreUpdate();


    }

    if(gameBall.y <= gameBall.height/2){
        gameBall.y = gameBall.height/2;
        bSpeedY *= -1;
    }

    else if(gameBall.y >= stage.stageHeight-gameBall.height/2){
        gameBall.y = stage.stageHeight-gameBall.height/2;
        bSpeedY *= -1;
    }
 //-------------------------------------------------------



    //keeps the player paddle within the stage
    //check if paddle is above top of the screen 
    if(playerPaddle.y - playerPaddle.height/2 < 0){
        playerPaddle.y = playerPaddle.height/2;
    } else if(playerPaddle.y + playerPaddle.hieght/2 > stage.stageHeight){
        playerPaddle.y = stage.stageHeight - playerPaddle.height/2;


    //check if paddle is below bottom of the screen
    } else if(playerPaddle.y + playerPaddle.height/2 > stage.stageHeight){
     playerPaddle.y = stage.stageHeight - playerPaddle.height/2;
  }

    }

If you only want your ball to be replaced with new one which has diffrent grphics and/or speed you can for example export your ball class to action script: 如果您只想将球替换为具有不同图形和/或速度的新球,则可以例如将球类导出到动作脚本:

Library>RMB on your symbol>Properties>ActionScript Linkage>Export for ActionScript

Type your class name under Class: field like "MyBallClass" and hit OK . Class:字段下输入您的班级名称,例如“ MyBallClass”,然后点击OK

Now you can construct this ball in your code and replace old one like this: 现在,您可以在代码中构造此球并替换旧的球,如下所示:

var newBall:MyBallClass = new MyBallClass();
addChild(newBall);
newBall.x = gameBall.x; newBall.y = gameBall.y;
gameBall = newBall;

Additionally you can define new variable like var speedModifier:Number = 1; 另外,您可以定义新变量,例如var speedModifier:Number = 1; to use with: 用于:

gameBall.x += bSpeedX * speedModifier;
gameBall.y += bSpeedY * speedModifier;

And change that also when you change the ball. 换球时也要改变它。

If You want to have multiple balls at same time You really should consider build this in OOP. 如果您想同时拥有多个球,那么您真的应该考虑在OOP中构建它。 For simplest example in addition to previous one You can create MyBallClass.as file and write in it something like: 对于除上一个示例以外的最简单示例,您可以创建MyBallClass.as文件并在其中写入类似以下内容的内容:

package  
{
    import flash.display.Sprite;
    import flash.geom.Point;

    public class MyBallClass extends Sprite
    {
        public var speedFactor:Number;
        public var speed:Point = new Point(-3.5, -2.5);
        public function MyBallClass(x:Number, y:Number, speedFactor:Number = 1) 
        {
            this.x = x; this.y = y;
            this.speed = speed;
        }
    }
}

Now you can create container for all the balls in yor game. 现在,您可以为游戏中的所有球创建容器。

var balls:Vector<MyBallClass> = Vector<MyBallClass>([]);

and run your physics for all of them in a loop. 并循环运行所有物理。

Generally main code would look something like this: 通常,主代码如下所示:

var balls:Vector.<MyBallClass> = Vector.<MyBallClass>([]);

addBall(...)//place first ball.

function loop(e:Event):void {
    processBalls();
    if(wantToAddNewSuperSpeedBall) addBall(x,y,3);
    ...
}

function processBalls() {
    for (var i:int = 0; i < balls.length; i++) {
        detecCollision(balls[i]);
        moveBall(balls[i]);
        //any code that process a single ball...
    }
}

function addBall(x:Number, y:Number, speedFactor:Number = 1) {
    var newBall:MyBallClass = new MyBallClass(x,y, speedFactor);
    addChild(newBall);
    balls.push(newBall);
}

function moveBall(ball:MyBallClass) {
    ball.x += ball.speed.x * ball.speedFactor;
    ball.y += ball.speed.y * ball.speedFactor;
}

So you should modify all functions which affect ball behavior to work with ball passed as argument, not only one specific instance and then use them for all balls. 因此,您应该修改影响球行为的所有函数,以便与作为参数传递的球一起使用,而不仅是一个特定的实例,然后将其用于所有球。

There are more to cover in this topic and this isn't maybe the best approach but I've tried to make it easy to understend. 在本主题中还有更多内容要讨论,这也许不是最好的方法,但是我试图使其易于理解。 There a lot of guides for OOP so you can get better idea about what is going on if you read them. 有很多关于OOP的指南,因此,如果您阅读它们,可以更好地了解发生了什么。 I hope that helped you somehow. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM