我的舞台上有对象char,我用main.as控制它,但是当我决定将它作为char.as并将其添加到main.as时成为错误..
main.as
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;
import flash.sampler.DeleteObjectSample;
import flashx.textLayout.utils.CharacterUtil;
public class main extends MovieClip
{
public function main()
{
var mychar:char = new char();
mychar.x = 60;
mychar.y = 60;
addChild(mychar);
}
}
}
char.as
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;
import flash.sampler.DeleteObjectSample;
public class char extends MovieClip
{
//char vars
var isRight:Boolean = false;
var isLeft:Boolean = false;
var isUp:Boolean = false;
var isDown:Boolean = false;
var angleDegree;
var angleRadian;
//stage vars
var objects = new Array(50);
public function char()
{
for (var i = 0; i<numChildren; i++) {
objects[i] = getChildAt(i);
}
addEventListener(Event.ENTER_FRAME, charEnterFrame);
addEventListener( MouseEvent.MOUSE_DOWN , MouseClick);
addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
function charEnterFrame(e:Event)
{
// **Char rotation
angleRadian=Math.atan2(mouseY-this.y,mouseX-this.x);
angleDegree = angleRadian * 180 / Math.PI;
this.rotation = angleDegree;
// **Char Movement
if (isRight==true)this.x += 5;
if (isLeft==true)this.x -= 5;
if (isUp==true)this.y -= 5;
if (isDown==true)this.y += 5;
}
function MouseClick(event:MouseEvent)
{
var radius = 30;
trace(objects.length);
var dx = radius * Math.cos(angleRadian);
var dy = radius * Math.sin(angleRadian);
var movex = 20 * Math.cos(angleRadian);
var movey = 20 * Math.sin(angleRadian);
var bullet = new Sprite();
bullet.graphics.beginFill(0x000000);
bullet.graphics.drawCircle(this.x+dx, this.y+dy, 3);
bullet.graphics.endFill();
var i = 1;
var f:Function;
addChild(bullet);
bullet.addEventListener(Event.ENTER_FRAME, f = function(){
bullet.x += movex*i;
bullet.y += movey*i;
for (var j = 0; j < objects.length-1; j++) {
if (bullet.hitTestObject(objects[j])) {
bullet.graphics.clear();
}
}
i++;
});
}
function KeyDown(event:KeyboardEvent):void
{
if (event.keyCode == 39)isRight = true;
if (event.keyCode == 37)isLeft = true;
if (event.keyCode == 38)isUp = true;
if (event.keyCode == 40)isDown = true;
}
function KeyUp(event:KeyboardEvent):void
{
if (event.keyCode == 39)isRight = false;
if (event.keyCode == 37)isLeft = false;
if (event.keyCode == 38)isUp = false;
if (event.keyCode == 40)isDown = false;
}
}
}
Flash结果:http://www.shareswf.com/media/games/swf/28063.swf