我正在开发一款游戏并且运行速度很快但是在让它运行几分钟之后就开始吃掉了CPU。记忆保持不变,我无法弄清楚原因。它可以来自矢量绘图吗?谢谢你的任何想法。
package {
import caurina.transitions.Tweener;
import com.freeactionscript.CollisionTest;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
[SWF(frameRate=60, width=640, height=960, backgroundColor=0x000000)]
public class Eye extends Sprite
{
public var Overlay:Sprite;
public var Taylor:ThePlayer;
public var ThePlayerPoint:Point;
public var Rolyat:TheEye;
public var EyeBullet:Bullet;
public var TheEyePoint:Point;
public var SearchPoint:Point;
public var LineOfSight:Sprite;
public var TheEyeSees:Boolean;
public var Barriers:Array;
public var BarrierPointL:Point;
public var BarrierPointR:Point;
public var Ground:int;
public var Blocking:int;
public var MousePoint:Point;
public var Blocked:Boolean;
public var Marker:Sprite;
public var CT:CollisionTest = new CollisionTest();
public function Eye()
{
/**
* Marker = new Sprite();
* Marker.graphics.beginFill(0x0C0C0C);
* Marker.graphics.drawCircle(0, 0, 5);
* Marker.graphics.endFill();
* addChild(Marker);
*/
Overlay = new Sprite();
Overlay.graphics.beginFill(0x000000);
Overlay.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
Overlay.graphics.endFill();
Overlay.alpha = 0;
addChild(Overlay);
Ground = 0.9 * stage.stageHeight;
graphics.lineStyle(1, 0x000000);
graphics.moveTo(0, Ground);
graphics.lineTo(stage.stageWidth, Ground);
Taylor = new ThePlayer();
ThePlayerPoint = new Point();
ThePlayerPoint.x = stage.stageWidth/2;
ThePlayerPoint.y = Ground - Taylor.height/2;
Taylor.x = ThePlayerPoint.x;
Taylor.y = ThePlayerPoint.y;
addChild(Taylor);
Rolyat = new TheEye(40, 40);
Rolyat.scaleX = 0.15;
Rolyat.scaleY = 0.15;
TheEyePoint = new Point();
TheEyePoint.x = stage.stageWidth/2;
TheEyePoint.y = 100;
Rolyat.x = TheEyePoint.x;
Rolyat.y = TheEyePoint.y;
addChild(Rolyat);
EyeBullet = new Bullet();
SearchPoint = new Point();
MousePoint = new Point();
LineOfSight = new Sprite();
addChild(LineOfSight);
Barriers = new Array(3);
for(var i:int = 0; i < Barriers.length; i ++) {
Barriers[i] = new Barrier();
if(i > 0) {
Barriers[i].x = Barriers[i - 1].x - 2 * Barriers[i - 1].width;
} else {
Barriers[i].x = -Barriers[i].width;
}
Barriers[i].y = RandomRange(Rolyat.y + 20, Ground - 20);
Barriers[i].Update();
addChild(Barriers[i]);
}
BarrierPointL = new Point();
BarrierPointR = new Point();
TheEyeSees = true;
stage.addEventListener(Event.ENTER_FRAME, Update);
Overlay.addEventListener(MouseEvent.CLICK, Click);
}
public function RandomRange(minNum:Number, maxNum:Number):Number {
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
//---------------------------------------------------------------
//Checks for intersection of Segment if as_seg is true.
//Checks for intersection of Line if as_seg is false.
//Return intersection of Segment AB and Segment EF as a Point
//Return null if there is no intersection
//---------------------------------------------------------------
public function lineIntersectLine(A:Point,B:Point,E:Point,F:Point,as_seg:Boolean=true):Point {
var ip:Point;
var a1:Number;
var a2:Number;
var b1:Number;
var b2:Number;
var c1:Number;
var c2:Number;
a1= B.y-A.y;
b1= A.x-B.x;
c1= B.x*A.y - A.x*B.y;
a2= F.y-E.y;
b2= E.x-F.x;
c2= F.x*E.y - E.x*F.y;
var denom:Number=a1*b2 - a2*b1;
if (denom == 0) {
return null;
}
ip=new Point();
ip.x=(b1*c2 - b2*c1)/denom;
ip.y=(a2*c1 - a1*c2)/denom;
//---------------------------------------------------
//Do checks to see if intersection to endpoints
//distance is longer than actual Segments.
//Return null if it is with any.
//---------------------------------------------------
if(as_seg){
if(Math.pow(ip.x - B.x, 2) + Math.pow(ip.y - B.y, 2) > Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2))
{
return null;
}
if(Math.pow(ip.x - A.x, 2) + Math.pow(ip.y - A.y, 2) > Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2))
{
return null;
}
if(Math.pow(ip.x - F.x, 2) + Math.pow(ip.y - F.y, 2) > Math.pow(E.x - F.x, 2) + Math.pow(E.y - F.y, 2))
{
return null;
}
if(Math.pow(ip.x - E.x, 2) + Math.pow(ip.y - E.y, 2) > Math.pow(E.x - F.x, 2) + Math.pow(E.y - F.y, 2))
{
return null;
}
}
return ip;
}
public function Click(m:MouseEvent):void {
MousePoint.x = m.localX;
MousePoint.y = m.localY;
Tweener.addTween(ThePlayerPoint, {x:m.localX, time:Point.distance(MousePoint, ThePlayerPoint)/stage.stageWidth, transition:"linear"});
}
public function UpdateThePlayer():void {
Taylor.x = ThePlayerPoint.x;
Taylor.y = ThePlayerPoint.y;
}
public function UpdateTheEye():void {
if(TheEyeSees) {
Rolyat.ScleraSprite.rotation = Rolyat.ScleraSprite.rotation = (180/Math.PI) * Math.atan(((stage.stageWidth/2) - Taylor.x)/(ThePlayerPoint.y - TheEyePoint.y));
TheEyeAttack();
} else {
if(Rolyat.TurnedRight == false) {
if(Rolyat.ScleraSprite.rotation <= 30) {
Rolyat.ScleraSprite.rotation += 0.8;
Rolyat.ScleraSprite.rotation += 0.8;
}
if(Rolyat.ScleraSprite.rotation > 30) {
Rolyat.TurnedRight = true;
}
} else {
if(Rolyat.ScleraSprite.rotation >= -30) {
Rolyat.ScleraSprite.rotation -= 0.8;
Rolyat.ScleraSprite.rotation -= 0.8;
}
if(Rolyat.ScleraSprite.rotation < -30) {
Rolyat.TurnedRight = false;
}
}
}
}
public function TheEyeAttack():void {
if(EyeBullet.Ready) {
EyeBullet.Ready = false;
EyeBullet.x = Rolyat.x;
EyeBullet.y = Rolyat.y;
addChild(EyeBullet);
Tweener.addTween(EyeBullet, {x:Taylor.x, y:Taylor.y, time:1, transition: "linear", onComplete:BulletExplode});
}
}
public function BulletExplode():void {
Tweener.addTween(EyeBullet, {scaleX:30, scaleY:30, time:0.25, transition:"linear", onComplete:RemoveBullet});
}
public function RemoveBullet():void {
Tweener.removeTweens(EyeBullet);
EyeBullet.scaleX = 1;
EyeBullet.scaleY = 1;
EyeBullet.Reset();
removeChild(EyeBullet);
}
//Draw LineOfSight to SearchPoint
public function UpdateLineOfSight():void {
LineOfSight.visible = false;
LineOfSight.graphics.clear();
if(TheEyeSees) {
LineOfSight.graphics.lineStyle(1, 0xFFFF00);
} else {
LineOfSight.graphics.lineStyle(1, 0x66CCCC);
}
SearchPoint.x = Math.tan(Math.PI/180 * (-Rolyat.ScleraSprite.rotation)) * (Ground - Rolyat.y) + stage.stageWidth/2;
SearchPoint.y = Ground;
LineOfSight.graphics.moveTo(Rolyat.x, Rolyat.y);
LineOfSight.graphics.lineTo(SearchPoint.x, SearchPoint.y);
}
//Check barrier collisions
public function CheckBarriers():void {
for(var j:int = 0; j < Barriers.length; j ++) {
BarrierPointL.x = Barriers[j].x;
BarrierPointL.y = Barriers[j].y;
BarrierPointR.x = Barriers[j].x + Barriers[j].width;
BarrierPointR.y = Barriers[j].y;
if(Barriers[j].Blocking == false && lineIntersectLine(TheEyePoint, ThePlayerPoint, BarrierPointL, BarrierPointR)) {
Barriers[j].Blocking = true;
Blocking ++;
} else if(Barriers[j].Blocking == true && !(lineIntersectLine(TheEyePoint, ThePlayerPoint, BarrierPointL, BarrierPointR))) {
Barriers[j].Blocking = false;
Blocking --;
}
}
if(Blocking == 0 && Math.abs(SearchPoint.x - Taylor.x) <= 50) {
TheEyeSees = true;
} else {
TheEyeSees = false;
}
}
//Move barriers and check collision
public function UpdateBarriers():void {
for(var j:int = 0; j < Barriers.length; j ++) {
Barriers[j].Update();
if(Barriers[j].x < stage.stageWidth + Barriers[j].width/2) {
Barriers[j].x += Barriers[j].Speed;
} else {
if(j == 0) {
Barriers[j].x = Barriers[Barriers.length - 1].x - 5 * Barriers[Barriers.length - 1].width;
} else {
Barriers[j].x = Barriers[j - 1].x - 5 * Barriers[j - 1].width;
}
Barriers[j].y = RandomRange(Rolyat.y + 20, Ground - 20);
}
CheckBarriers();
}
}
public function Update(e:Event):void {
setChildIndex(Overlay, numChildren - 1);
UpdateThePlayer();
UpdateTheEye();
UpdateLineOfSight();
UpdateBarriers();
}
}
}