我制作的游戏有点问题。你看,我想阻止我的角色穿过我放置的平台,但由于我的角色实例是在我的角色类中的另一个类中,我无法访问其速度变量等等。我很坦率地不确定如何解决这个问题。
这是角色类。
package com.classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import com.senocular.utils.KeyO
bject;
import flash.ui.Keyboard;
import flash.events.Event;
public class Gubbe extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
private var speed:Number = 0.6;
private var vx:Number = 0;
private var vy:Number = 0;
private var gravity:Number = .4;
private var maxspeed:Number = 21;
private var maxspeedG:Number = 50
private var friction:Number = .92;
private var nospeed = 0;
private var fall:Boolean = false;
private var platt:platform;
public function Gubbe(stageRef:Stage) : void {
this.stageRef = stageRef;
key = new KeyObject(stageRef);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
//Knapptryck
vy += gravity;
y += vy;
x += vx;
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
//Positions updatering
x += vx;
y += vy;
if(vx > 0)
fall=true;
if(vy > 0)
fall=true;
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*.4);
}
else if (y < 0)
{
y = 0;
vy = -vy;
}
}
}
}
这是Engine类,其中包含角色和平台实例。
package com.classes{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import com.classes.Gubbe;
public class Engine extends MovieClip
{
public var ourGubbe:Gubbe;
public function Engine() : void
{
ourGubbe = new Gubbe(stage);
ourGubbe.x = stage.stageWidth / 2;
ourGubbe.y = stage.stageHeight / 2;
stage.addChild(ourGubbe);
stage.addEventListener(Event.ENTER_FRAME, land);
}
public function land(e:Event) : void
{
if(ourGubbe.hitTestObject(platform))
{
}
}
}
}
我在想hitTestObject,但我不知道如何在与墙碰撞时使角色实例停止。我会感激我能得到任何帮助!
答案 0 :(得分:0)
你只需要为你的角色添加一些公共布尔值,如:
public var leftfree:Boolean = true;
public var rightfree:Boolean = true;
然后你需要在你的引擎类中为角色的左右两侧添加2个不同的hittest检查,如:
if(ourGubbe.hitTestObject(platform))
{
if(platform.x > ourGubbe.x) ourGubbe.rightfree = false;
else ourGubbe.leftfree = false,
}
else
{
ourGubbe.leftfree = true;
ourGubbe.rightfree = true;
}
然后你可以在你的角色类中使用那个布尔值来&#34;向左或向右移动1个像素(取决于你的角色移动速度,使用实际移动速度而不是1个像素)在与碰撞侧相关的每个帧中防止它进入平台。
答案 1 :(得分:0)
我建议使用Getters和Setters。这样你就可以从其他类中获取变量。
在Gubbe中你会添加:
public function getXVelocity():Number
{
return vx;
}
public function setXVelocity(value:Number):void
{
//Check if value is within the set limits.
if (value > maxspeed)
value = maxspeed;
else if (value < -maxspeed)
value = -maxspeed
vx = value;
}
public function getYVelocity():Number
{
return vy;
}
public function setYVelocity(value:Number):void
{
//Check if value is within the set limits.
if (value > maxspeed)
value = maxspeed;
else if (value < -maxspeed)
value = -maxspeed
vy = value;
}
正如你所看到的那样,你可以在setter中建立一些安全性,就像我现在使用maxspeed一样。
然后在引擎中你可以做这样的事情:
if (ourGubbe.hitTestObject(platform))
{
//Get the current velocity.
var ySpeed:Number = ourGubbe.getYVelocity();
//Move Gubbe back so it is outside the platform again.
ourGubbe.y -= ySpeed;
//Set the y velocity to 0.
ourGubbe.setYVelocity(0);
}
碰撞处理仅作为示例。不要只使用上面的碰撞处理,否则你的角色会表现出一些奇怪的行为。
祝你好运!