HUD not properly calculating things
alright, made a simple dodge the astroids game to get a little more
practiced in flash, and using document classes and whathaveyou. Well
through some painful playing around I got the score to increase by one for
each astroid you have avoided, sadly though the force field which is
supposed to go from 100 down to 0 (havnt gotten into what will haven when
it reaches 0 yet) seems to be adding arbitrary ammounts to it (I added a
trace to read it in real time like I had done for the score, but it reads
as [object textfield]. Please help.
package GameFiles.SourceCode.Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class User extends MovieClip
{
private var numStars:int = 65;
public static var enemyList:Array = new Array();
private var ourShip:Ship;
private var scoreHUD:ScoreHUD;
public function User() : void
{
var ourShip:Ship = new Ship(stage);
stage.addChild(ourShip);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
ourShip.addEventListener("hit", shipHit, false, 0, true);
scoreHUD = new ScoreHUD(stage);
stage.addChild(scoreHUD);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));;
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 90) <= 5)
{
var enemy:Astroid = new Astroid(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy,
false, 0, true);
enemy.addEventListener("Passed", enemyPassed, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
public function shipHit(e:Event) : void
{
scoreHUD.updateHits(e.currentTarget.points);
}
public function enemyPassed(e:Event) : void
{
scoreHUD.updateScore(1);
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
}
that is the main class
the HUD is this
package GameFiles.SourceCode.Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.events.Event;
public class ScoreHUD extends MovieClip
{
private var stageRef:Stage;
public var s_score:Number = 0;
public var s_hits:Number = 0;
public function ScoreHUD(stageRef:Stage)
{
this.stageRef = stageRef;
x = 25;
y = 350;
hits.text = "100";
score.text = "0";
}
public function updateScore(value:Number) : void
{
s_score += value;
score.text = String(s_score);
}
public function updateHits(value:Number) : void
{
s_hits -= value;
trace(hits);
hits.text = String(s_score);
}
}
and within the main it calls to ship to get the value of the loss, ship is
here.
package GameFiles.SourceCode.Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import GameFiles.SourceCode.Classes.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
public class Ship extends MovieClip {
private var stageRef:Stage;
private var key:KeyObject;
private var speed:Number = 0.5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.93;
private var maxspeed:Number = 8;
public var points:int = 0.1;
public function Ship(stageRef:Stage)
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
if (currentLabel == "default")
{
stop()
}
if (currentLabel == "Done")
{
gotoAndStop("default")
}
if (key.isDown(Keyboard.LEFT))
vx -= speed;
else if (key.isDown(Keyboard.RIGHT))
vx += speed;
else
vx *= friction;
if (key.isDown(Keyboard.UP))
vy -= speed;
else if (key.isDown(Keyboard.DOWN))
vy += speed;
else
vy *= friction;
x += vx;
y += vy;
rotation = vx + 1;
if (vx > maxspeed)
vx = maxspeed;
else if (vx < -maxspeed)
vx = -maxspeed;
if (vy > maxspeed)
vy = maxspeed;
else if (vy < -maxspeed)
vy = -maxspeed;
if (x > stageRef.stageWidth)
{
x = stageRef.stageWidth;
vx = -vx;
}
else if (x < 0)
{
x = 0;
vx = -vx;
}
if (y > stageRef.stageHeight)
{
y = stageRef.stageHeight;
vy = -vy;
}
else if (y < 0)
{
y = 0;
vy = -vy;
}
for (var i:int = 0; i < User.enemyList.length; i++)
{
if (hitTestObject(User.enemyList[i].hit))
{
trace("hitEnemy");
gotoAndPlay("ForcefieldOn");
dispatchEvent(new Event("hit"))
User.enemyList[i].takeHit();
}
}
}
}
No comments:
Post a Comment