在平台游戏中射击子弹

时间:2014-07-18 05:36:03

标签: actionscript-3 flash

我正在尝试制作一个玩家可以投球的平台游戏。

奇怪的是我的代码似乎射杀了子弹,但我无法弄清楚为什么它不会从我的播放器中射击。

已经过了几个小时,我仍然不明白这是什么问题。

问题在于:

[网址= http://fr.tinypic.com/r/i5ry1i/8]My视频[/ URL] 图片 : enter image description here enter image description here

这是我的代码:

在我的Main.as

public class Main extends MovieClip {
private var bulletList:Array = new Array();

public function keyDownFunction(event:KeyboardEvent) {
...
.
} else if (event.keyCode == 70) {
                fireBullet();}
..
}

public function fireBullet():void
{

    var bullet:Bullet = new Bullet(hero.mc.x, hero.mc.y, playerDirection);
    addChild(bullet);
    trace("addChild Bullet");

    bullet.addEventListener(Event.REMOVED, bulletRemoved);
    bulletList.push(bullet);

}

在我的Bullet.as中:

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Stage;

    public class Bullet extends MovieClip {

        private var speed:int = 30;
        private var initialX:int;

        public function Bullet(heroX:int, heroY:int, playerDirection:String) {
            trace("bullet called");
            trace(heroY);
            trace(heroX);
            // constructor code
            if(playerDirection == "left") {
                speed = -30; //speed is faster if player is running
                x = heroX - 25;
            } else if(playerDirection == "right") {
                speed = 30;
                x = heroX + 25
            }
            y = heroY;

            initialX = x; //use this to remember the initial spawn point

        }

你知道我的代码中可能出现什么问题吗?

非常感谢你的帮助。


修改

以下是我所做的改变:

在我的Main.as

public function fireBullet():void
{
    var heroP:Point = new Point(hero.mc.x, hero.mc.y);
    heroP = localToLocal(hero.mc, this, heroP);
    var bullet:Bullet = new Bullet(heroP.x, heroP.y, playerDirection);
    addChild(bullet);
    trace("addChild Bullet");

    bullet.addEventListener(Event.REMOVED, bulletRemoved);
    bulletList.push(bullet);

}
public function localToLocal(from:DisplayObject, to:DisplayObject, origin:Point):Point
{
    if (!to || !from) return new Point();

    return to.globalToLocal(from.localToGlobal(origin));
}

我的Bullet.as

没有改变

第二次编辑

为了理解发生了什么,我放了一些“痕迹”,我必须说我根本不明白! 子弹仍未对齐!

在我的Main.as中,我将“trace(hero.mc.y)”放入fireBullet

        public function fireBullet():void
{
    var heroP:Point = new Point(hero.mc.x, hero.mc.y);
    var bullet:Bullet = new Bullet(heroP.x, heroP.y, playerDirection);
    heroP = localToLocal(hero.mc, this, heroP);
    addChild(bullet);


    trace(hero.mc.y);


    trace("addChild Bullet");
    bullet.addEventListener(Event.REMOVED, bulletRemoved);
    bulletList.push(bullet);
}

在我的Bullet.as中我放了“trace(heroPXY)”。

public function Bullet(heroPX:int, heroPXY:int, playerDirection:String) {
            trace("bullet called");
            // constructor code
            if(playerDirection == "left") {
                speed = -30; //speed is faster if player is running
                x =  heroPX;
            } else if(playerDirection == "right") {
                speed = 30;
                x =  heroPX;
            }
            y = heroPXY;


            trace(heroPXY);



            initialX = x; //use this to remember the initial spawn point
            addEventListener(Event.ENTER_FRAME, loop);
        }

跟踪(heroPXY)和跟踪(hero.mc.y)的结果完全相同!那么为什么子弹没有对齐?

1 个答案:

答案 0 :(得分:1)

您可能需要转换坐标空间,例如通过使用以下功能:

/**
 * <p>converts coordinates of clip "from" to coordinate space of "to" clip</p>
 * @param   from
 * @param   to
 * @param   origin
 * @return
 */
public function localToLocal(from:DisplayObject, to:DisplayObject, origin:Point):Point
{
    if (!to || !from) return new Point();

    return to.globalToLocal(from.localToGlobal(origin));
}

用法:

var heroP:Point = new Point(hero.mc.x, hero.mc.y);
heroP = localToLocal(here.mc, this, heroP);
var bullet:Bullet = new Bullet(heroP.x, heroP.y, playerDirection);
    addChild(bullet);

这应该将此处的mc坐标空间转换为this的坐标空间,在此处添加此项目符号。