Flash游戏创建分数 - 动态文本字段(AS3)

时间:2010-09-22 22:02:28

标签: flash

创建一个类似pacman的游戏,玩家在这里玩物体。当MovieClip(方框)在舞台上吃圆圈时,动态文本字段中的分数应该上升10个点。

收到错误: line:`scoreField.text =“”+ string(currentScore);

错误消息:调用可能未定义的方法字符串。

 package {
   import flash.display.Sprite;
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.ui.Keyboard;

   public class Move extends MovieClip {

   //var ScoreObjects:Array = new Array(); // creates ScoreObjects array
      var circle:MovieClip;


      private var keyRight:Boolean=false;
      private var keyLeft:Boolean=false;
      private var keyForward:Boolean=false;
      private var keyBackward:Boolean=false;
      private var forwardMove:int=0;
      private var sideMove:int=0;

      private var inertia:int=8; //amount of friction

   //var score_field:String;
   //var point:MovieClip;
   //private var playerScore:int;

   var currentScore:int;


 // Constructor--------------------------------------------------------------------
      public function Move() {
         init();
      }

 // function init -----------------------------------------------------------------
   function init():void {

         //stage.frameRate=60;
    //var score_field:String="";

  /*ScoreObjects[0] = new Circle();
  ScoreObjects[0].amount = 1; // amount of point -- not sure
  ScoreObjects[0].name = "circle";*/
  circle.amount = 10; // each circle is worth 10 points

  //var playerScore:int = 0;
  var currentScore:int = 0;

         stage.addEventListener(Event.ENTER_FRAME, frameloop);
         stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
         stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);

    box.addEventListener(Event.ENTER_FRAME, eatCircle);
    wall.addEventListener(Event.ENTER_FRAME, hitWall);



    //stage.addChild(ScoreObjects[0]); // add Score Objects to stage ------------------------------
    //trace(ScoreObjects[0]);

    /*ScoreObjects[0].x = 105;
    ScoreObjects[0].y = 233;*/

      }

 // function eatCircle --------------------------------------------------------------
 function eatCircle(event:Event):void {

  if (box.hitTestObject(circle)) {
    trace ("I ate the circle");
    stage.removeChild(circle);
    var newPoint:int;
    newPoint = circle.amount;
    // point = ScoreObjects[0].amount //store property's value of amount in variable...?
    calcPoints(newPoint);
    //box.deleteEventListener(Event.ENTER_FRAME, eatCircle)
    //calcScore();
    //playerScore++;
   } else {
    trace ("I didn't eat the circle");
   }
 }

 function calcPoints(newPoint:int):void {
  currentScore += newPoint;
  updateDisplayScore(currentScore); 
 }

 function updateDisplayScore(currentScore:int) {
   scoreField.text = " " + String(currentScore);
   var displayText:String="";   
  displayText = scoreField.text; // assigning the message to the field*/
 }



 // function hitWall --------------------------------------------------------------
 function hitWall(event:Event):void {
  if (box.hitTestObject(wall)) {
    box.y+=6;
   } else if (box.hitTestObject(wall2)) {
    box.y-=6;

   } else if (box.hitTestObject(wall3)) { 
    box.x+=6;

   } else if (box.hitTestObject(wall4)) {
    box.x-=6;
      }

 }

 // function keyDownEvent ------------------------------------------------------------
     function keyDownEvent(event:KeyboardEvent):void {
         switch (event.keyCode) {
            case Keyboard.UP:
               keyForward = true;
               keyBackward = false;
               break;

            case Keyboard.DOWN:
               keyBackward = true;
               keyForward = false;
               break;

            case Keyboard.LEFT:
               keyLeft = true;
               keyRight = false;
               break;

            case Keyboard.RIGHT:
               keyRight = true;
               keyLeft = false;
               break;
         }
      }

 // function keyUpEvent ------------------------------------------------------------
     function keyUpEvent(event:KeyboardEvent):void {
         switch (event.keyCode) {

            case Keyboard.UP:
               keyForward = false;
               break;

            case Keyboard.DOWN:
               keyBackward = false;
               break;

            case Keyboard.LEFT:
               keyLeft = false;
               break;

            case Keyboard.RIGHT:
               keyRight = false;
               break;
         }
      }

 // function frameloop ------------------------------------------------------------
     function frameloop(event:Event):void {

         //amount of force
         if (keyForward) {
            forwardMove -=2;
         }
         if (keyBackward) {
            forwardMove += 2;
         }
         if (keyLeft) {
            sideMove -= 2;
         }
         if (keyRight) {
            sideMove += 2;
         }

         forwardMove +=(0-forwardMove)/inertia;
         sideMove +=(0-sideMove)/inertia;

         box.y+=forwardMove;
         box.x+=sideMove;

   //point = e.currentTarget.amount;


      } 

   }// end of class
}// end of package

2 个答案:

答案 0 :(得分:2)

scoreField.text = " " + string(currentScore);

应该是

scoreField.text = " " + String(currentScore);

答案 1 :(得分:0)

scoreField.text = "" + String(currentScore);

将起作用,您也可以使用

scoreField.text = " " + currentScore.toString();