你好,请你帮我做啧啧... 这是tut网站http://as3gametuts.com/2012/06/20/platformer-12/comment-page-1/#comment-800
而且我不知道子弹与敌人发生碰撞的代码问题..这是它的代码..
function fireBullet():void {
var playerDirection:String;
if(player.scaleX < 0){
playerDirection = "left";
} else if(player.scaleX > 0){
playerDirection = "right";
}
var bullet:Bullet = new Bullet(player.x - scrollX, player.y - scrollY, playerDirection, xSpeed);
back.addChild(bullet);
bulletList.push(bullet);
bullet.addEventListener(Event.REMOVED, bulletRemoved);
}
var bulletList:Array = new Array();
function bulletRemoved(e:Event):void {
e.currentTarget.removeEventListener(Event.REMOVED, bulletRemoved); //this just removes the eventListener so we don't get an error
bulletList.splice(bulletList.indexOf(e.currentTarget), 1); //this removes 1 object from the bulletList, at the index of whatever object caused this function to activate
}
//bullet
//enemies
var enemyList:Array = new Array();
function addEnemy(xLocation:int, yLocation:int):void{
var enemy:Enemy = new Enemy(xLocation, yLocation);
back.addChild(enemy);
enemy.addEventListener(Event.REMOVED, enemyRemoved);
enemyList.push(enemy);
}
function addEnemy1(xLocation:int, yLocation:int):void{
var enemy1:Enemy1 = new Enemy1(xLocation, yLocation);
back.addChild(enemy1);
enemy1.addEventListener(Event.REMOVED, enemyRemoved);
enemyList.push(enemy1);
}
function enemyRemoved(e:Event):void{
e.currentTarget.removeEventListener(Event.REMOVED, enemyRemoved); //this just removes the eventListener so we don't get an error
enemyList.splice(enemyList.indexOf(e.currentTarget), 1); //this removes 1 object from the enemyList, at the index of whatever object caused this function to activate
}
addEnemiesToLevel1();
function addEnemiesToLevel1():void{
addEnemy(272.8, 19.8);
addEnemy(959, 278.8);
addEnemy1(374.1, 305.1);
addEnemy1(966, 29);
}
这是主循环
//main loop
function loop(e:Event):void{
if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
/*
if(back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
//added upbump
if(back.hitTestPoint(player.x + upBumpPoint1.x, player.y + upBumpPoint1.y, true)){
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if(back.hitTestPoint(player.x + upBumpPoint2.x, player.y + upBumpPoint2.y, true)){
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
*/
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(back.collisions.hitTestPoint(player.x + downBumpPoint1.x, player.y + downBumpPoint1.y, true)){
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
player.scaleX = -1
} else if(rightPressed){
xSpeed += speedConstant;
player.scaleX = 1
}
/*if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
/*up bump
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.3;
}
}
*/
if(downBumping){ //if we are touching the floor
if(ySpeed > 0){
ySpeed = 0;
}
if(upPressed){ //and if the up arrow is pressed
ySpeed = jumpConstant; //set the y speed to the jump constant
}
} else {
ySpeed += gravityConstant;
}
//max speed
if(xSpeed > maxSpeedConstant){ //moving right
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){ //moving left
xSpeed = (maxSpeedConstant * -1);
}
//min sped
if(Math.abs(xSpeed) < 0.5){
xSpeed = 0;
}
//keycollection
if(keyCollected == false){ // if we still haven't collected the key
if(player.hitTestObject(back.other.doorKey)){ // and if the player collides with the key
back.other.doorKey.visible = false; // hide the key from view
keyCollected = true; // set our Boolean to true
//key score
score += 40;
}
updateScore(score);
}
if(doorOpen == false){ // if the door hasn't been opened yet
if(keyCollected == true){ // and if the player has already collected the key
if(player.hitTestObject(back.other.lockedDoor)){ // check if the door and the player are touching
// if all of these conditions are met...
back.other.lockedDoor.gotoAndStop(2); // ...switch the door's image to its 2nd frame
doorOpen = true; // ...set the variable to true
}
}
}
xSpeed *= friction;
ySpeed *= friction;
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
mount.x=scrollX * 0.6;
mount.y=scrollY * 0.6;
sky.x = scrollX * 0.4;
sky.y = scrollY * 0.4;
//movement logic
if(player.currentLabel != animationState){
player.gotoAndStop(animationState);
}
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping){
animationState = "running";
}
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping){
animationState = "running";
} else if(downBumping){
animationState = "idle";
} else {
animationState = "jumping";
}
if (enemyList.length > 0) // if there are any enemies left in the enemyList{
for (var i:int = 0; i < enemyList.length; i++) // for each enemy in the enemyList
{
if (bulletList.length > 0) // if there are any bullets alive
{
for (var j:int = 0; j < bulletList.length; j++) // for each bullet in the bulletList
{
if ( enemyList[i].hitTestObject(bulletList[j]) )
{
trace("Bullet and Enemy are colliding");
enemyList[i].removeSelf();
bulletList[j].removeSelf();
}
// enemyList[i] will give you the current enemy
// bulletList[j] will give you the current bullet
// this will check all combinations of bullets and enemies
// and see if any are colliding
}
}
}
}
}
答案 0 :(得分:1)
我想我可以回答我自己的问题.. -_-
放enemy.addEventListener(Event.REMOVED, enemyRemoved); on
'function addEnemy(xLocation:int, yLocation:int):void{
不合适,因为它会立即将数组转为0;所以对象将无法与之碰撞..我将被移除的敌人转移到了主循环中..它工作得很好..