Creating a pacman-like game, where a player eats objects. When a MovieClip (box) eats a circle on the stage, the score in the dynamic text field should go up by 10 points.
Receiving an error: line: `scoreField.text = " " + string(currentScore);
error message: Call to a possibly undefined method string.
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 --开发者_JAVA技巧------------------------------------------------------------
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
scoreField.text = " " + string(currentScore);
should be
scoreField.text = " " + String(currentScore);
scoreField.text = "" + String(currentScore);
will work and you can also use
scoreField.text = " " + currentScore.toString();
精彩评论