Actionscript 3 as3非均匀地形的坡度检测
我想创建一个2d游戏,其中一辆汽车沿着不规则形状的山的边缘移动。我想使用基本as3(没有像Box2d或Nape这样的物理引擎)。通过大量的研究,我找到了我想要的东西,但只有逻辑,没有源代码。有人能帮我用一段代码在as3中实现这一点吗?还建议是否有更好的替代方案来获得所需的输出。我自己对这样的代码不太了解,我不能发表评论,因为我缺少7个声誉:'(但是你链接的文章确实有另一个指向蠕虫类程序的链接,其中的代码可能对你有所帮助。一个指向该网站的链接,我还将提供一些适当的代码,你可以查看它是否对你有用。) 所以这个程序有一个监听器,如果任何键是向下的,它会将布尔值设置为true,如果它是你的程序正在使用的键(移动、跳跃空间或其他),监听器看起来是这样的Actionscript 3 as3非均匀地形的坡度检测,actionscript-3,flash,terrain,Actionscript 3,Flash,Terrain,我想创建一个2d游戏,其中一辆汽车沿着不规则形状的山的边缘移动。我想使用基本as3(没有像Box2d或Nape这样的物理引擎)。通过大量的研究,我找到了我想要的东西,但只有逻辑,没有源代码。有人能帮我用一段代码在as3中实现这一点吗?还建议是否有更好的替代方案来获得所需的输出。我自己对这样的代码不太了解,我不能发表评论,因为我缺少7个声誉:'(但是你链接的文章确实有另一个指向蠕虫类程序的链接,其中的代码可能对你有所帮助。一个指向该网站的链接,我还将提供一些适当的代码,你可以查看它是否对你有用。)
public function key_down(e:KeyboardEvent) {
if (e.keyCode==37) {
left_key=true;
}
if (e.keyCode==39) {
right_key=true;
}
if (e.keyCode==32) {
space_key=true;
}
}
然后,当一个键被释放时,它有一个后续的侦听器
public function key_up(e:KeyboardEvent) {
if (e.keyCode==37) {
left_key=false;
}
if (e.keyCode==39) {
right_key=false;
}
if (e.keyCode==32) {
space_key=false;
}
}
最后,stage有一个ENTER_FRAME侦听器,用于移动运行此函数的角色
public function move_character(e:Event) {
//If left key is pressed, we'll move the character to the left
if (left_key) {
for (i=0; i<3; i++) {//Do you remember when we made the character fall? We had to move the character pixel by pixel
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-6,character.y-10,1,17))) {
character.x--; /*If the character doesn't hit the ground, we can move left. However,
the character may be sunk under the ground. We have to lift it*/
while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y--;
}
}
}
}
if (right_key) {//Well, that's the same for the right key
for (i=0; i<3; i++) {
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x+5,character.y-10,1,17))) {
character.x++;
while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y--;
}
}
}
}
if (space_key&&! jumping) {//That's easy: if he isn't jumping and you press space, his speed will be negative and he'll jump
character_speed=-10;
jumping=true;//Now the character can't jump again
}
character_speed++;//Every frame we will increase character's speed
if (character_speed>0) {
//If the speed is positive, we will check a collision between the terrain and the rectangle below the character
for (i=0; i<character_speed; i++) {//We check the collision pixel by pixel...
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y++;//If there isn't a collision, the character will fall
} else {
jumping=false;//If there's a collision with the ground, the character isn't jumping
character_speed=0;//The speed is 0, because the character hit the ground
}
}
} else {
for (i=0; i<Math.abs(character_speed); i++) {//If the speed is negative, the for loop won't work. We have to use Math.abs().
//Now we will check the collision between the terrain and the rectangle above the character
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y-10,10,1))) {
character.y--;
} else {
character_speed=0;//Well, that's the same: the character hit the ground
}
}
}
}
公共函数移动字符(e:事件){
//如果按左键,我们将向左移动角色
如果(左键){
对于(i=0;i我自己对这样的代码没有太多的了解,我不能发表评论,因为我缺少7个声誉:'(但是你链接的文章确实有另一个指向蠕虫类程序的链接,其中的代码可能对你有所帮助。一个指向该网站的链接,我还将提供一些适当的代码,你可以查看它是否对你有用。)
所以这个程序有一个监听器,如果任何键是向下的,它会将布尔值设置为true,如果它是你的程序正在使用的键(移动、跳跃空间或其他),监听器看起来是这样的
public function key_down(e:KeyboardEvent) {
if (e.keyCode==37) {
left_key=true;
}
if (e.keyCode==39) {
right_key=true;
}
if (e.keyCode==32) {
space_key=true;
}
}
然后,当一个键被释放时,它有一个后续的侦听器
public function key_up(e:KeyboardEvent) {
if (e.keyCode==37) {
left_key=false;
}
if (e.keyCode==39) {
right_key=false;
}
if (e.keyCode==32) {
space_key=false;
}
}
最后,stage有一个ENTER_FRAME侦听器,用于移动运行此函数的角色
public function move_character(e:Event) {
//If left key is pressed, we'll move the character to the left
if (left_key) {
for (i=0; i<3; i++) {//Do you remember when we made the character fall? We had to move the character pixel by pixel
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-6,character.y-10,1,17))) {
character.x--; /*If the character doesn't hit the ground, we can move left. However,
the character may be sunk under the ground. We have to lift it*/
while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y--;
}
}
}
}
if (right_key) {//Well, that's the same for the right key
for (i=0; i<3; i++) {
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x+5,character.y-10,1,17))) {
character.x++;
while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y--;
}
}
}
}
if (space_key&&! jumping) {//That's easy: if he isn't jumping and you press space, his speed will be negative and he'll jump
character_speed=-10;
jumping=true;//Now the character can't jump again
}
character_speed++;//Every frame we will increase character's speed
if (character_speed>0) {
//If the speed is positive, we will check a collision between the terrain and the rectangle below the character
for (i=0; i<character_speed; i++) {//We check the collision pixel by pixel...
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y++;//If there isn't a collision, the character will fall
} else {
jumping=false;//If there's a collision with the ground, the character isn't jumping
character_speed=0;//The speed is 0, because the character hit the ground
}
}
} else {
for (i=0; i<Math.abs(character_speed); i++) {//If the speed is negative, the for loop won't work. We have to use Math.abs().
//Now we will check the collision between the terrain and the rectangle above the character
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y-10,10,1))) {
character.y--;
} else {
character_speed=0;//Well, that's the same: the character hit the ground
}
}
}
}
公共函数移动字符(e:事件){
//如果按左键,我们将向左移动角色
如果(左键){
对于(i=0;i