Actionscript 3 Flash AS3:构造函数必须是实例方法
大家好,我有个问题,不知道怎么解决:(有人能告诉我怎么做吗 构造函数必须是实例方法 这是我的代码:Actionscript 3 Flash AS3:构造函数必须是实例方法,actionscript-3,flash,hittest,Actionscript 3,Flash,Hittest,大家好,我有个问题,不知道怎么解决:(有人能告诉我怎么做吗 构造函数必须是实例方法 这是我的代码: package { import com.coreyoneil.collision.CollisionList; import flash.events.Event; import flash.display.Sprite; public class terrain extends Sprite { private var whee
package
{
import com.coreyoneil.collision.CollisionList;
import flash.events.Event;
import flash.display.Sprite;
public class terrain extends Sprite
{
private var wheel:Ball;
private var collisionList:CollisionList;
private var speed:Number;
private const GRAVITY:Number = .75;
private const FRICTION:Number = .98;
private const IMMOVABLE:Number = 10000;
public function terrain():void
{
if(stage == null)
{
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
addEventListener(Event.REMOVED_FROM_STAGE, clean, false, 0, true);
}
else
{
init();
}
}
private function init(e:Event = null):void
{
collisionList = new CollisionList(terrain);
wheel = new wheel(10);
wheel.mass = IMMOVABLE * 2;
addChild(wheel);
collisionList.addItem(wheel);
wheel.x = 30;
wheel.y = 10;
speed = 0;
terrain.graphics.lineStyle(15);
addEventListener(Event.ENTER_FRAME, updateScene);
}
private function updateScene(e:Event):void
{
var collisions:Array = collisionList.checkCollisions();
if(collisions.length)
{
var collision:Object = collisions[0];
var angle:Number = collision.angle;
var overlap:int = collision.overlapping.length;
var sin:Number = Math.sin(angle);
var cos:Number = Math.cos(angle);
var vx0:Number = wheel.vx * cos + wheel.vy * sin;
var vy0:Number = wheel.vy * cos - wheel.vx * sin;
// Unlike the other examples, here I'm choosing to calculate the amount
// of bounce based on the objects' masses, with a default mass of 10000 (IMMOVABLE)
// being used for the drawing the wheel is colliding with. As such, the only
// real variable in play here is the current vector of the wheel.
vx0 = ((wheel.mass - IMMOVABLE) * vx0) / (wheel.mass + IMMOVABLE);
wheel.vx = vx0 * cos - vy0 * sin;
wheel.vy = vy0 * cos + vx0 * sin;
wheel.vx -= cos * overlap /wheel.radius;
wheel.vy -= sin * overlap / wheel.radius;
wheel.vx += speed;
}
trace("down");
wheel.vy += GRAVITY;
wheel.vy *= FRICTION;
wheel.vx *= FRICTION;
wheel.x += wheel.vx;
wheel.y += wheel.vy;
if(wheel.x > stage.stageWidth) wheel.x = stage.stageWidth;
if(wheel.x < 0) wheel.x = 0;
if(wheel.y > stage.stageHeight - (wheel.height >> 1))
{
wheel.y = 10;
wheel.x = 30;
wheel.vx = wheel.vy = 0;
}
}
private function clean(e:Event):void
{
removeEventListener(Event.ENTER_FRAME, updateScene);
}
}
}
包
{
导入com.coreyoneil.collision.CollisionList;
导入flash.events.Event;
导入flash.display.Sprite;
公共级地形扩展精灵
{
私人车轮:球;
私有var冲突列表:冲突列表;
私人var速度:数字;
私有常量重力:数字=0.75;
私人康斯特摩擦:数字=0.98;
私人建筑不动产:数量=10000;
公共功能地形():void
{
if(stage==null)
{
addEventListener(Event.ADDED_TO_STAGE,init,false,0,true);
addEventListener(Event.REMOVED_FROM_STAGE,clean,false,0,true);
}
其他的
{
init();
}
}
私有函数init(e:Event=null):void
{
碰撞列表=新的碰撞列表(地形);
车轮=新车轮(10);
车轮质量=不可移动*2;
addChild(车轮);
碰撞列表。附加项(车轮);
车轮x=30;
车轮y=10;
速度=0;
地形.图形.线型(15);
addEventListener(Event.ENTER_FRAME,updateScene);
}
私有函数updateScene(e:事件):void
{
变量冲突:数组=collisionList.checkCollisions();
if(冲突.长度)
{
变量碰撞:对象=碰撞[0];
变量角度:数字=碰撞角度;
变量重叠:int=collision.overlapping.length;
变量sin:Number=Math.sin(角度);
变量cos:Number=Math.cos(角度);
变量vx0:Number=wheel.vx*cos+wheel.vy*sin;
变量vy0:Number=wheel.vy*cos-wheel.vx*sin;
//与其他示例不同,这里我选择计算数量
//基于对象质量的反弹,默认质量为10000(不可移动)
//用于绘图时,轮子与之碰撞。因此,只有
//这里真正起作用的变量是车轮的当前矢量。
vx0=((车轮质量-不可移动)*vx0)/(车轮质量+不可移动);
wheel.vx=vx0*cos-vy0*sin;
wheel.vy=vy0*cos+vx0*sin;
wheel.vx-=cos*重叠/wheel.radius;
wheel.vy-=sin*重叠/wheel.radius;
wheel.vx+=速度;
}
追踪;
wheel.vy+=重力;
wheel.vy*=摩擦力;
wheel.vx*=摩擦力;
wheel.x+=wheel.vx;
wheel.y+=wheel.vy;
如果(wheel.x>stage.stageWidth)wheel.x=stage.stageWidth;
如果(wheel.x<0)wheel.x=0;
如果(车轮y>舞台高度-(车轮高度>>1))
{
车轮y=10;
车轮x=30;
wheel.vx=wheel.vy=0;
}
}
专用功能清除(e:事件):无效
{
removeEventListener(Event.ENTER_FRAME,updateScene);
}
}
}
代码中有一些注释..请忽略它,我已经使用了示例
collisionList = new CollisionList(terrain);
terrain.graphics.lineStyle(15);
这是错误1026,如果构造函数是静态的、私有的或在您的情况下用作标识符,也会引发此错误。请使用This.graphics而不是terrain.graphics或仅使用graphics.etc(删除地形),并将“This”作为“CollisionList”的参数传递。
(不相关:最好以大写“地形”开头命名类) 这是错误1026,如果构造函数是静态的、私有的或在您的情况下用作标识符,也会引发此错误。请使用This.graphics而不是terrain.graphics或仅使用graphics.etc(删除地形),并将“This”作为“CollisionList”的参数传递。
(不相关:最好以大写“Terrain”开头命名类)你的问题是什么?你有什么问题吗?如果(stage==null),不必设置
if
签入构造函数,因为此时将创建一个新对象,在其构造完成之前无法将其添加到stage。因此,stage属性在构造函数中始终为null。只需添加侦听器就足够了。其他部分将永远不会被计算。您的问题是什么?您有什么问题u有问题?没有必要使if(stage==null)
签入构造函数,因为此时将创建一个新对象,在其构造完成之前无法将其添加到stage中。因此,stage属性在构造函数中始终为null。只需添加侦听器就足够了。其他部分将永远不会被计算。感谢您的建议,但如果我告诉我们这一点我在第3行(我开始导入东西的地方)仍然会遇到同样的错误。哦,是的,还有另一个错误:“collisionList=new collisionList(terrain);”,也将其更改为new collisionList(this)。基本上,如果类名具有静态属性或方法且来自另一个类,则只需使用类名作为标识符,否则它在类内为“this”或“nothing”。感谢您的建议,但如果我使用它,我仍然会在第3行(开始导入内容)中遇到相同的错误。哦,是的,还有另一个错误:“collisionList=new collisionList(terrain);”,也将其更改为new collisionList(this)。基本上,如果类名具有静态属性或方法且来自另一个类,则只需使用类名作为标识符,否则在类中使用“this”或“nothing”。