在 ActionScript 中制作一个具有 2D 运动并在撞到墙壁时停止的对象

发布于 2024-08-03 12:02:26 字数 102 浏览 7 评论 0原文

我想制作一个可以进行键盘移动(上、下、左、右)的正方形,并且当它碰到另一个物体(例如墙壁)时会停止。

编辑:我已经有一个正方形和一个键盘布局,但如果需要特定的东西,请告诉我!

I want To make a square that has keyboard movement (up, down, left, right) and will stop when it hits another object such as a wall.

EDIT: I already have a square and a keyboard layout but if that needs to be something specific then please tell me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-08-10 12:02:27

杰克逊,你需要做的就是

  1. 监听按键
  2. 更新你的角色
  3. 检查碰撞

你不是很具体,但我百分百肯定,如果你用谷歌更多地了解你需要的东西,你会找到它,因为有大量的闪存游戏教程。

这是一个最小的设置,

//needed to update the position
var velocityX:Number = 0;
var velocityY:Number = 0;
//draw the ball
var ball:Sprite = new Sprite();
ball.graphics.beginFill(0);
ball.graphics.drawCircle(0,0,20);
ball.graphics.endFill();
addChild(ball);
ball.x = ball.y = 100;
//setup keys
stage.addEventListener(KeyboardEvent.KEY_DOWN, updateBall);
function updateBall(event:KeyboardEvent):void{
    switch(event.keyCode){
        case Keyboard.RIGHT:
        if(velocityX < 6) velocityX += .25;
        break;
        case Keyboard.LEFT:
        if(velocityX > -6) velocityX -= .25;
        break;
        case Keyboard.DOWN:
        if(velocityY < 6) velocityY += .25;
        break;
        case Keyboard.UP:
        if(velocityY > -6) velocityY -= .25;
        break;
    }
    //update ball position
    ball.x += velocityX;
    ball.y += velocityY;
    //check walls , if collision, flip direction
    if(ball.x > stage.stageWidth || ball.x < 0) velocityX *= -1;
    if(ball.y > stage.stageHeight|| ball.y < 0) velocityY *= -1;
}

显然它并不理想,但它很基本,并且很容易说明顶部的点状态。
您可能想要使用一些平滑的按键并在 EnterFrame 上更新您的游戏。

祝你好运

jackson, all you need to do is

  1. Listen for keys
  2. update your character
  3. check for colissions

You are not beeing specific, but I am 100% sure that if you google a bit more into what you need you will find it as there are tons of flash gaming tutorials.

Here is a minimal setup

//needed to update the position
var velocityX:Number = 0;
var velocityY:Number = 0;
//draw the ball
var ball:Sprite = new Sprite();
ball.graphics.beginFill(0);
ball.graphics.drawCircle(0,0,20);
ball.graphics.endFill();
addChild(ball);
ball.x = ball.y = 100;
//setup keys
stage.addEventListener(KeyboardEvent.KEY_DOWN, updateBall);
function updateBall(event:KeyboardEvent):void{
    switch(event.keyCode){
        case Keyboard.RIGHT:
        if(velocityX < 6) velocityX += .25;
        break;
        case Keyboard.LEFT:
        if(velocityX > -6) velocityX -= .25;
        break;
        case Keyboard.DOWN:
        if(velocityY < 6) velocityY += .25;
        break;
        case Keyboard.UP:
        if(velocityY > -6) velocityY -= .25;
        break;
    }
    //update ball position
    ball.x += velocityX;
    ball.y += velocityY;
    //check walls , if collision, flip direction
    if(ball.x > stage.stageWidth || ball.x < 0) velocityX *= -1;
    if(ball.y > stage.stageHeight|| ball.y < 0) velocityY *= -1;
}

Oviously it's not ideal, but it's basic and it illustrates the points states at the top easily.
You might want to use some smooth keys and update your game onEnterFrame.

Goodluck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文