大家好,我只是一个小问题:如何让它看起来像我的" map"对象(平台)在添加到舞台并在自己的类中定义时是移动(滚动)?我在代码中有一些滚动文本,但它一直给我一个错误,说我正在访问一个空对象的属性。然后,我尝试在课堂上对该对象进行定义,但这并没有帮助。谁能告诉我我做错了什么?
package AS {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Point;
public class Bro extends MovieClip
{
//define your class determining variables
private var moveUp:Boolean;
private var moveLeft:Boolean;
private var moveDown:Boolean;
private var moveRight:Boolean;
private var Jumping:Boolean;
private var moveSpeedR:uint;
private var moveSpeedY:Number;
private var hitTestRadius:Number;
private var hitTestAngle:Number;
private var ConvertFrametoSecond:Number;
private var checker:uint;
private var barrier:Obstacles;
private var DSTtoMC:Number;
private var fluidity:int;
private var Varoffset:int;
private var varUNO:int;
private var varDOS:int;
public function Bro() {
init();
}
protected function init(){
//moveUp=false;
moveLeft=false;
//moveDown=false;
moveRight=false;
//Jumping == false;
moveSpeedR=2;
moveSpeedY =0;
hitTestRadius=29.35;/*58.7/2*/
ConvertFrametoSecond = 1/30;
checker = 0;
DSTtoMC = this.x -((Constants.stageRef.stageWidth/2) + Varoffset);
fluidity = 5;
Varoffset = 10;
varUNO = DSTtoMC/fluidity;
varDOS = DSTtoMC/fluidity;
Constants.stageRef.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedListener);
Constants.stageRef.addEventListener(KeyboardEvent.KEY_UP,keyReleasedListener);
Constants.stageRef.addEventListener(Event.ENTER_FRAME,frameListener);
Constants.stageRef.addEventListener(Event.ENTER_FRAME, Jump);
Constants.stageRef.addEventListener(Event.ENTER_FRAME, Scrolling);
Constants.stageRef.addEventListener(Event.ENTER_FRAME, Initiate);
}
//when you press down a key
private function keyPressedListener(e:KeyboardEvent){
var key:uint=e.keyCode;
if(key==87||key==38)
{//W / UP
moveUp=true;
}
else if(key==65||key==37)
{//A / LEFT
moveLeft=true;
}
else if(key==68||key==39){//D / RIGHT
moveRight=true;
}
else if (key == 32)
{
moveSpeedY = -15;
}
}
//when you release a key
private function keyReleasedListener(e:KeyboardEvent){
var key:uint=e.keyCode;
/*if(key==87||key==38)
{//W / UP
moveUp=false;
}*/
/*else*/ if(key==65||key==37)
{//A / LEFT
moveLeft=false;
}
else if(key==68||key==39)
{//D / RIGHT
moveRight=false;
}
}
//every frame (30 times a second)
private function frameListener(e:Event)
{
/*if(moveUp){
this.y-=moveSpeedR;
}*/
if(moveLeft){
this.x-=moveSpeedR;
}
if(moveRight){
this.x+=moveSpeedR;
}
while(hitTestPointAngle(Constants.wallsRef, this.x, this.y, hitTestRadius, 180)){
if(hitTestAngle>180)
{
this.x-=0.125*Math.sin(Math.PI/180*hitTestAngle);
this.y+=0.125*Math.cos(Math.PI/180*hitTestAngle);
}
else
{
this.x+=0.125*Math.sin(Math.PI/180*hitTestAngle);
this.y-=0.125*Math.cos(Math.PI/180*hitTestAngle);
}
}
}
/**/private function Scrolling(e:Event)
{
//finding the player's position in respect to the center of the stage
//stands for distance to main character
//add in a "ground" later; focus on the canvas for now
if(DSTtoMC < 0)
{
DSTtoMC *=-1;
}
if(this.x < (Constants.stageRef.stageWidth/2))
{
barrier.x += varUNO;
this.x += varUNO;
/*Mysterybox.x += varUNO;
MonsSprite.x += varUNO;
BarSprite.x += varUNO;*/
}
//possibly change back to regular if statement; might interfere with initial if statement
else if(this.x > (Constants.stageRef.stageWidth/2))
{
barrier.x -= varDOS;
this.x -= varDOS;
/*Mysterybox.x -= varDOS;
MonsSprite.x -= varDOS;
BarSprite.x -= varDOS;*/
}
}
private function Initiate(Angled:Event):void
{
if(Constants.wallsRef.hitTestPoint(this.x, this.y+hitTestRadius-0.125,false)/*0.015625&& Jumping == false*/)
{
Constants.stageRef.addEventListener(Event.ENTER_FRAME, Jump);
}
}
private function Jump(Angled:Event):void
{
//the player is not allowed to double jump
Jumping = true;
//when character leaves the ground
//This statement makes it so that the speed is constantly decreasing as a result of gravity.
moveSpeedY += ConvertFrametoSecond *(9.8)/*gravity constant*/;
//(1/2)*a*t^2
this.y += moveSpeedY;
if(Constants.wallsRef.hitTestPoint(this.x, this.y+hitTestRadius-.125,true)/*& Jumping == true*/)
{
moveSpeedY= 0;
//this resets the person on top of the "ground"
//this.y = 366.6 - this.height;
//Constants.stageRef.removeEventListener(Event.ENTER_FRAME,Jump);
//now the player can jump again
Jumping = false;
}
//This value eventually deccelerates upward until it hits its maximum. From this point it will accelerate back downward
//}
}
private function hitTestPointAngle(barrier:Obstacles, xPos:Number, yPos:Number, radius:Number, samples:uint):Boolean{
const SAMPLE:Number=0.5/samples;
var dx:Number;
var dy:Number;
var angle:Number;
//number of points that hit the boxes
var hits:Array=[];
var i:int=samples;
while(i>0){
i--;
angle =(2*Math.PI)*(i*SAMPLE);
dx = radius*Math.cos(angle);
dy = radius*Math.sin(angle);
if(barrier.hitTestPoint(xPos + dx, yPos + dy, true)){
hits.push(new Point(dx,dy));
}
}
if(hits.length==0){
return false;
}
var tx:Number=0;
var ty:Number=0;
i=hits.length;
while(i>0){
i--;
tx+=hits[i].x;
ty+=hits[i].y;
}
hitTestAngle=Math.atan2(ty,tx)*(180/Math.PI)-90;
return true;
}
}}
这是我的常数类:
package AS{
import flash.display.Stage;
public class Constants {
public static var stageRef:Stage;
public static var wallsRef:Obstacles;
}}