2015-05-15 175 views
0

我一直在創建一個對象類文件中的命中檢測函數,所以如果它碰到舞臺上的播放器,它會檢測到它。不過,我從文檔類文件調用的公共變量得到「錯誤:1067訪問未定義的屬性」。 這裏是Object類中的功能我想打電話給公共變量:Actionscript:將公共變量從一個類傳遞到另一個類?

public function hitTesting(obj:Object) { 

    if (hitTestPoint(obj.x + leftBumpPoint.x, obj.y + pubVarsleftBumpPoint.y, true)) { 
     trace("leftCollide"); 
     leftCollide = true; 


    } else { 
     leftCollide = false; 
    } 

    if (hitTestPoint(obj.x + rightBumpPoint.x, obj.y + rightBumpPoint.y, true)) { 
     trace("right hit"); 
     rightCollide = true; 
    } else { 
     rightCollide = false; 
    } 

    if (hitTestPoint(obj.x + upBumpPoint.x, obj.y + upBumpPoint.y, true)) { 
     trace("up hit"); 
     upCollide = true; 
    } else { 
     upCollide = false; 
    } 

} 

編輯: 代碼文檔類:

package { 

    import flash.events.Event; 
    import flash.events.KeyboardEvent; 
    import flash.events.*; 
    import flash.ui.Keyboard; 
    import flash.display.*; 
    import flash.utils.Timer; 
    import flash.sampler.NewObjectSample; 
    import flash.geom.Point; 
    import MediumEnemy; 

    public class PublicVariables extends MovieClip { 

     public var upPressed: Boolean = false; 
     public var downPressed: Boolean = false; 
     public var leftPressed: Boolean = false; 
     public var rightPressed: Boolean = false; 
     public var xSpeed: Number = 0; 
     public var ySpeed: Number = 0; 
     public var gravity: Number = 1; 
     public var scrollX: Number = 0; 
     public var scrollY: Number = 0; 
     public var speedConstant: int = 8; 
     public var friction: Number = 0.55; 
     public var leftCollide: Boolean = false; 
     public var rightCollide: Boolean = false; 
     public var upCollide: Boolean = false; 
     public var downCollide: 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, 80); 
     public var jumpConstant: Number = -65; 
     public var gravityConstant: Number = 5; 


     public function PublicVariables() { 

代碼對象類:

package { 

    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.TimerEvent; 
    import flash.utils.Timer; 
    import flash.geom.Point; 
    import PublicVariables; 


    public class MediumEnemy extends PublicVariables { 
     //private var xSpeed: Number = 8 //randomValue(3, 6); //not being used 
     //private var ySpeed: Number = 8 //randomValue(8, 15); not being used 
     private var direction: Boolean = false; 



     public function MediumEnemy(xLocation: Number, yLocation: Number) { 

      x = xLocation + randomValue(0, 550); 
      y = yLocation; //set x position to random value 


      if (x < 275) //determine whether to make the direction of motion left or right 
      { 
       direction = true 
       //trace("go left") 

      } else { 
       direction = false; 
       //trace("go right"); 
      } 



      this.addEventListener(Event.ENTER_FRAME, animate); //add this 
     } 



      function animate(event: Event) { //looping code 

       switch (direction) { 
        case false: 
         { 
          x -= xSpeed; 
          break; 
         } 
        case true: //go right 
         { 
          x += xSpeed; 

          break; 
         } 
       } 
       y += ySpeed; 


} 





      public function hitTesting(obj: Object) { 

      if (hitTestPoint(obj.x + leftBumpPoint.x, obj.y + leftBumpPoint.y, true)) { 
       trace("leftCollide"); 
       leftCollide = true; 


      } else { 
       leftCollide = false; 
      } 

      if (hitTestPoint(obj.x + rightBumpPoint.x, obj.y + rightBumpPoint.y, true)) { 
       trace("right hit"); 
       rightCollide = true; 
      } else { 
       rightCollide = false; 
      } 

      if (hitTestPoint(obj.x + upBumpPoint.x, obj.y + upBumpPoint.y, true)) { 
       trace("up hit"); 
       upCollide = true; 
      } else { 
       upCollide = false; 
      } 

     } 


    } 

} 

僅供參考:我打算以某種方式將'player'對象傳入hitTesting函數以使用參數獲取其x位置。

+0

你能分享一些更多的代碼?瞭解您希望能夠發送/接收數據的兩個類的結構將是必要的。 – gabriel

+0

我已經編輯它來包含變量定義和東西。有相當多的代碼,所以如果你需要更多的問題。非常感謝你的回覆。 –

+0

你可以嘗試改變這一行:public class MediumEnemy將MovieClip擴展到公共類MediumEnemy擴展PublicVariables?讓我知道你是否會得到你正在尋找的東西。 – gabriel

回答

1

建議你一個好辦法,有必要來分析你的代碼,並瞭解可能導致的問題,等等。

試試這個:

public class MediumEnemy extends MovieClip { 
    private var direction:Boolean = false; 
    private var publicVariables:PublicVariables = new PublicVariables(); 

    public function MediumEnemy(xLocation:Number, yLocation:Number) { 
     x = xLocation + randomValue(0, 550); 
     y = yLocation; // set x position to random value 

     if (x < 275) // determine whether to make the direction of motion left or right 
     { 
      direction = true; 
      // trace("go left") 
     } else { 
      direction = false; 
      // trace("go right"); 
     } 

     this.addEventListener(Event.ENTER_FRAME, animate); // add this 
    } 

    private function animate(event:Event) { // looping code 
     switch (direction) { 
      case false: { 
       x -= publicVariables.xSpeed; 
       break; 
      } 
      case true: // go right 
      { 
       x += publicVariables.xSpeed; 

       break; 
      } 
     } 
     y += publicVariables.ySpeed; 
    } 

    public function hitTesting(obj:Object) { 
     if (hitTestPoint(obj.x + publicVariables.leftBumpPoint.x, obj.y + publicVariables.leftBumpPoint.y, true)) { 
      trace("leftCollide"); 
      publicVariables.leftCollide = true; 
     } else { 
      publicVariables.leftCollide = false; 
     } 

     if (hitTestPoint(obj.x + publicVariables.rightBumpPoint.x, obj.y + publicVariables.rightBumpPoint.y, true)) { 
      trace("right hit"); 
      publicVariables.rightCollide = true; 
     } else { 
      publicVariables.rightCollide = false; 
     } 

     if (hitTestPoint(obj.x + publicVariables.upBumpPoint.x, obj.y + publicVariables.upBumpPoint.y, true)) { 
      trace("up hit"); 
      publicVariables.upCollide = true; 
     } else { 
      publicVariables.upCollide = false; 
     } 
    } 

} 
+0

現在它說.swf顯然包含無效數據。再次感謝您的回覆。 :) –

+0

將您的代碼編輯爲原始狀態並分析我的示例,以瞭解MediumEnemy和PublicVariables之間的交互如何工作,以便能夠對代碼進行適當更改。 – gabriel

相關問題