1

编辑:我现在包含了一个 Player.as 和一个 addchild

我整天都在努力了解如何做到这一点,并在这样做的过程中再次学到了很多东西。但我已经到了需要帮助的地步。

我知道我必须这样做:在 Back1 类中创建一个 Collisions 变量。

因为名为 Back1 的背景是包含碰撞图像的影片剪辑

我找到了一个或 2 个很好的站点,可以很好地解释变量和类,但我仍然不知道应该如何解决这个问题

变量和类之后的研究:

http://www.republicofcode.com/tutorials/flash/as3variables/

http://www.photonstorm.com/archives/1136/flash-game-dev-tip-1-creating-a-cross-game-communications-structure

上述问题导致以下错误,但我认为这是由于未在 Back1 类中创建 Collisions var

ArgumentError:错误 #1063:Bumper() 上的参数计数不匹配。预期:2,值 0。

  • 在 flash.display::MovieClip/gotoAndStop() 在 DocumentClass/onRequestStart()DocumentClass.as:64] 在 flash.events::EventDispatcher/dispatchEventFunction() 在 flash.events::EventDispatcher/dispatchEvent() 在 MenuScreen/onClickStart( )MenuScreen.as:18]

    package 
     {
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.geom.Point;
    import Bumper;
    //import Back1;
    
    public class Test extends MovieClip
    {
    
        public var leftBumping:Boolean = false;
    public var rightBumping:Boolean = false;
    public var upBumping:Boolean = false;
    public var downBumping:Boolean = false;
    
    public var leftBumpPoint:Point = new Point(-30,-55);
    public var rightBumpPoint:Point = new Point(30,-55);
    public var upBumpPoint:Point = new Point(0,-120);
    public var downBumpPoint:Point = new Point(0,0);
    
    public var scrollX:Number = 0;
    public var scrollY:Number = 500;
    
    public var xSpeed:Number = 0;
    public var ySpeed:Number = 0;
    
    public var speedConstant:Number = 4;
    public var frictionConstant:Number = 0.9;
    public var gravityConstant:Number = 1.8;
    public var jumpConstant:Number = -35;
    public var maxSpeedConstant:Number = 18;
    
    public var doubleJumpReady:Boolean = false;
    public var upReleasedInAir:Boolean = false;
    
    public var keyCollected:Boolean = false;
    public var doorOpen:Boolean = false;
    
    public var currentLevel:int = 1;
    
    public var animationState:String = "idle";
    
    public var bulletList:Array = new Array();
    public var enemyList:Array = new Array();
    public var bumperList:Array = new Array();
     public var back1:Back1;
    public var collisions:Collisions;
    //public var back1:Collisions = new Collisions ;
     public var player:Player;
    
    public function Test()
    {
        addEventListener(Event.ADDED_TO_STAGE, init);
    }
    
    public function init(e:Event):void
    {
    
        player = new Player(320, 360);
        back1 = new Back1();
        collisions = new Collisions();
        //back1.collisions = new Collisons();
              addBumpersToLevel1();
    }
    public function addBumpersToLevel1():void
    {
        addBumper(500, -115);
        addBumper(740, -115);
    }
            public function addPlayerTolevel1():void 
    {
        addPlayer(320, 360);
    }
    public function loop(e:Event):void
    {
        trace("back1.collisions "+back1.collisions);
                    trace("back1 "+back1);
                    trace("collisions "+collisions);
        if (back1.collisions.hitTestPoint(player.x + leftBumpPoint.x,player.y + leftBumpPoint.y,true))
        {
    

以防万一我添加了 Bumper.as

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

public class Bumper extends MovieClip{
    public function Bumper(xLocation:int, yLocation:int) {
        // constructor code
        x = xLocation;
        y = yLocation;

        addEventListener(Event.ENTER_FRAME, bumper);
    }

    public function bumper(e:Event):void{
        //code here
    }
}

}

播放器.as

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

    public class Player extends MovieClip {

        public function Player(xLocation:int, yLocation:int) {
            // constructor code
            x = xLocation;
            y = yLocation;
           }



     //   public function removeSelf():void {
      //      trace("remove enemy");
       //     removeEventListener(Event.ENTER_FRAME, loop);
       //     this.parent.removeChild(this);
       // }



 }
 }

Back1.as 文件(注意它必须被错误地实例化)

package  {
import flash.display.MovieClip;


public class Back1 extends MovieClip {
    //public var collisions:Back1;
           //what should i put here?
}
}
4

2 回答 2

1

我不确定我是否完全理解你的意思。这个问题措辞很奇怪。

我假设您想在背景对象(Back 类)和播放器对象之间实现碰撞?我无法从您发布的代码中看到播放器对象是什么,因为您的 Test 类中没有这样的变量。

要测试两个对象之间的碰撞检查,请使用以下代码:

if(someObject.hitTestObject(anotherObject))

或者在您使用 hitTestPoint 时:

if(back1.hitTestPoint(player.x, player.y,true))

再说一次,我不知道您发布的代码中 back1 类的外观。如果它扩展了 MovieClip 或 Sprite,并且您有一个执行相同操作的 Player 类(或任何 DisplayObject),这应该可以工作。

这: Bumper() 上的参数计数不匹配。预期:2,值 0。

您得到的错误似乎来自代码中未显示的另一个地方。我假设您没有将任何参数传递给 Bumper 类的构造函数。

顺便说一句,这是 Flash IDE 示例还是其他程序,例如 FlashDevelop 或 FlashBuilder?如果您使用 Flash IDE 并尝试将代码附加到放置在场景中的影片剪辑实例,我认为不可能将参数传递给它。抱歉,自从我在 Flash IDE 中工作以来已经有一段时间了。

编辑:

这是一些示例代码:

//::  Change Back1 class to this

package  {
import flash.display.MovieClip;


public class Back1 extends MovieClip {
    public function Back1()
    {
        graphics.beginFill(0xFF0000);
        graphics.drawRect(0, 0, 50, 50);
        graphics.endFill();
    }
}
}


//::  Then in your Main class (Or the Test class) add the following

var player:Player = new Player(25, 25);
var collidable:Back1 = new Back1();

addChild(player);
addChild(collidable);

//::  Goes in your loop/update
if (collidable.hitTestPoint(player.x, player.y, true))
{
    trace("HIT PLAYER");
}

如何将图形应用到 Back1 类取决于您,我只是画了一个简单的框。它可以是任何东西。

于 2013-01-16T13:16:35.927 回答
0

为 Bumper 类设置默认参数:

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

    public class Bumper extends MovieClip{
        public function Bumper(xLocation:int = 0, yLocation:int = 0) {
            // constructor code
            x = xLocation;
            y = yLocation;

            addEventListener(Event.ENTER_FRAME, bumper);
        }

        public function bumper(e:Event):void{
            //code here
        }
    }

    }
于 2013-01-16T13:29:35.570 回答