我在Flash Develop as3中创建了一个简单的2D平台游戏,但我不知道如何为我的角色创造一种跳跃方式。
以下是 main.as 的代码:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
/**
* ...
* @author Harry
*/
public class Main extends Sprite
{
public var StartButton:Go;
public var FireBoy:Hero;
public var WaterGirl:Female;
public var Door1:Firedoor;
public var Door2:Waterdoor;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
StartButton = new Go();
addChild(StartButton);
StartButton.addEventListener(MouseEvent.CLICK, startgame);
}
private function startgame(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
removeChild(StartButton);
FireBoy = new Hero ();
stage.addChild(FireBoy);
FireBoy.y = 495;
//This allows movement for FireBoy
stage.addEventListener(KeyboardEvent.KEY_DOWN, HandleHeroMove);
WaterGirl = new Female();
stage.addChild(WaterGirl);
WaterGirl.x = 70;
WaterGirl.y = 495;
stage.addEventListener(KeyboardEvent.KEY_DOWN, HandleFemaleMove);
Door1 = new Firedoor();
stage.addChild(Door1);
Door1.x = 5;
Door1.y = 62;
Door2 = new Waterdoor();
stage.addChild(Door2);
Door2.x = 100;
Door2.y = 62;
graphics.beginFill(0x804000, 1);
graphics.drawRect(0, 0, 800, 40);
graphics.endFill();
graphics.beginFill(0x804000, 1);
graphics.drawRect(0, 170, 600, 40);
graphics.endFill();
graphics.beginFill(0x804000, 1);
graphics.moveTo(800, 200);
graphics.lineTo(800, 700);
graphics.lineTo(400, 700);
graphics.lineTo(100, 700);
graphics.endFill();
graphics.beginFill(0x804000, 1);
graphics.drawRect(0, 580, 800, 40);
graphics.endFill();
}
//This handles FireBoys movement
public function HandleHeroMove(e:KeyboardEvent):void
{
trace(e.keyCode);
//This is for moving to the left
if (e.keyCode == 37)
{
FireBoy.x = FireBoy.x - 30;
Check_Border();
}
//This is for moving to the right
else if (e.keyCode == 39)
{
FireBoy.x = FireBoy.x + 30;
Check_Border();
}
else if (e.keyCode == 38)
{
FireBoy.grav = -15;
}
}
//This handles WaterGirls movement
public function HandleFemaleMove (e:KeyboardEvent):void
{
trace(e.keyCode);
//This is for moving to the left
if (e.keyCode == 65)
{
WaterGirl.x = WaterGirl.x - 30;
Check_Border();
}
//This is for moving to the right
else if (e.keyCode == 68)
{
WaterGirl.x = WaterGirl.x + 30;
Check_Border();
}
else if (e.keyCode == 87)
{
WaterGirl.grav = -15;
}
}
//This stops characters from leaving the screen
public function Check_Border():void
{
if (FireBoy.x <= 0)
{
FireBoy.x = 0;
}
else if (FireBoy.x > 750)
{
FireBoy.x = 750;
}
if (WaterGirl.x <= 0)
{
WaterGirl.x = 0;
}
else if (WaterGirl.x > 750)
{
WaterGirl.x = 750;
}
}
}
}
以下是 Hero.as 的代码(我的Female.as代码相同):
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
/**
* ...
* @author Harry
*/
public class Hero extends Sprite
{
[Embed(source="../assets/FireBoy.jpg")]
private static const HeroFireBoy:Class;
private var FireBoy:Bitmap;
public var grav:int = 0;
public var floor:int = 580;
public function Hero()
{
FireBoy = new Hero.HeroFireBoy();
scaleX = 0.1;
scaleY = 0.1;
addChild(FireBoy);
}
public function adjust():void
{
FireBoy.y += grav;
if(FireBoy.y+FireBoy.height/2<floor)
grav++;
else
{
grav = 0;
FireBoy.y = floor - FireBoy.height / 2;
}
if (FireBoy.x - FireBoy.width / 2 < 0)
FireBoy.x = FireBoy.width / 2;
if (FireBoy.x + FireBoy.width / 2 > 800)
FireBoy.x = 800 - FireBoy.width / 2;
}
}
}
请您准确建议我应该写什么代码以及放在哪里,因为我找不到任何有用的东西,而且我对此感到非常紧张。
答案 0 :(得分:0)
一种方法是这样的:在你的Hero.as中,你的功能如jump,move_left和move_right,并保持坐标在Hero类中。然后在实例化Hero对象的任何地方调用这些方法。
如main.as中的示例,当按下空格键或向上箭头键以使你的英雄跳跃时,你可以执行此代码:
var hero:Hero = new Hero();
hero.jump();
如果是Y轴上的简单跳转,则只需更改Y轴值即可。如果它同时是X轴和Y轴,就像向前跳转一样,那么您可以考虑使用三角函数正弦函数来计算角色应该移动的点以使其看起来平滑。
你不需要相同的女性课程。你应该做的是:创建一个类(可能名为Character)。将jump,move_left,move_right函数添加到此类。然后继承Hero.as和Female.as中的Character类。 (你需要谷歌&#34;继承&#34;以了解更多相关信息)。然后,您可以使用驻留在Character类中的相同代码来实例化Hero和Female。
您可能还需要在Hero.as和Female.as中重写:
[Embed(source="../assets/FireBoy.jpg")]
private static const HeroFireBoy:Class;
在你的Hero.as和Female.as中,分配正确的图像。