我正在制作简单的Flash游戏。 想法:当点击空格按钮字符必须跳跃时,通过单击(左/右箭头)在地面上行走的绿色角色。现在角色留在地上,走路,但没有与墙壁碰撞(穿过墙壁)。 在这部分代码中,我试图与ground3墙碰撞,但它不起作用,角色穿过墙壁,如果我跳到地面3(停留在顶部)我无法向右移动,因为它触及地面3 。我需要做的是,如果角色与墙右侧接触,则将速度设置为0。
以下是代码的一部分(向右移动):
if(right){
Hero.x_speed = walkspeed;
setDirection(0);
trace("right");
/////////////////////SOMETHING WRONG HERE//////////////////////////////
if (ground3.hitTestPoint((centrasXright), centrasY, true)){
trace("HIT THE WALL/GROUND with RIGHT"); //always get this message when stay on the ground 3
Hero.x_speed = 0;
}
else {
trace("Do not touch anything");
}
这是我的游戏照片:
以下是我的变数:
private var gravity:Number = 0.01;
private const max_speed:int = 8;
private const walkspeed:int = 4;
private const jumpspeed:int = 10;
private const start_x:int = 50;
private const start_y:int = 300;
private var left:Boolean;
private var up:Boolean;
private var right:Boolean;
private var space:Boolean;
private var ground:Ground = new Ground;
private var Hero:hero = new hero;
以下是我的代码的一部分:
private function create_hero(){
addChild(Hero);
Hero.x = start_x;
Hero.y = ground.y-30;
Hero.x_speed = 0;
Hero.y_speed = 0;
}
private function setDirection(param) {
if (param == 0) {
Hero.scaleX = 1;
} else {
Hero.scaleX = -1;
}
}
private function update_hero(){
Hero.y_speed += gravity;
if(left){
Hero.x_speed = -walkspeed;
setDirection(1);
}
var test:MovieClip = new MovieClip;
test.x = Hero.x;
test.y = Hero.y;
var centrasXright:Number = test.x + (Hero.width/2);
var centrasXleft:Number = test.x - (Hero.width/2);
var centrasY:Number = test.y + (Hero.height/2);
if(right){
Hero.x_speed = walkspeed;
setDirection(0);
trace("right");
if (ground3.hitTestPoint((centrasXright), centrasY, true)){
trace("HIT THE WALL/GROUND with RIGHT");
Hero.x_speed = 0;
}
else {
trace("Do not touch anything");
}
}
if(up && Hero_col.can_jump){
Hero.y_speed = -jumpspeed;
}
if(Hero.y_speed > max_speed){
Hero.y_speed = max_speed;
}
///////////////////////////////////////////////////////////////////
////////////////HERE IS COLLISION WITH THE TOP OF GROUND///////////
///////////////////////////////////////////////////////////////////
if(Hero.y_speed>0 && Hero.hitTestObject(ground) || Hero.hitTestObject(ground3) ){
Hero.y_speed=0;
if(space){
trace("Clicked SPACE");
Hero.y_speed += gravity;
Hero.y -= 80;
}
else {
}
}
forecast_y = Hero.y + Hero.y_speed;
forecast_x = Hero.x + Hero.x_speed;
Hero_col.solve_all(forecast_x, forecast_y);
Hero.x_speed = 0;
}