2013-04-03 66 views
0

我試過讓spritesheet移動一會兒,並嘗試各種方式,在easeljs文檔。儘管如此,我仍然無法讓血腥的事情發揮作用。所以我希望你們中的一些人可以看看它。createjs spritesheet不是動畫

我使用的spritesheet是160x40,其中1個sprite是40x40。

這裏是應有的動畫我的敵人功能:(25行十歲上下是創建在移動功能有一個gotoAndPlay。)

var enemyWidth = 40; 
var enemyHeight = 40; 
function Enemy(number) { 
    this.number = number; 
    this.id = 'enemy'; 
    var randomStartLoc = new locationOutsidePlayfield(); 

    enemyLoc = new locationOutsidePlayfield(); 
    this.x = randomStartLoc.x; 
    this.y = randomStartLoc.y; 

    this.health = 100; 

    //The damage a collision the enemy with a player does 
    this.damage = 30; 

    var target = new Target(this.x, this.y); 
    this.velX = target.velX; 
    this.velY = target.velY; 

    this.hitScore = 25; 
    this.killScore = this.hitScore*2; 


    var spriteSheet = new createjs.SpriteSheet({ 
     images: ["resources/sprites/enemy_spritesheet.png"], 
     frames: {width:40, height:40}, 
     animations: { 
      walk: [0, 3] 
     } 
    }); 

    this.sprite = new createjs.BitmapAnimation(spriteSheet); 
    this.sprite.currentFrame = 0; 

    this.sprite = new createjs.BitmapAnimation(spriteSheet); 

    this.sprite.x = this.x; 
    this.sprite.y = this.y; 
    this.sprite.width = enemyWidth; 
    this.sprite.height = enemyHeight; 


    this.collide = function(arrayIndex, bullet) { 
     if (bullet) { 
      this.health -= player.damage; 
     } 

     if (this.health <= 0) { 
      //Generate a number between 0 and 10. If it's 1(10% chance) a powerup will be made on the spot of the death. 
      var percentage = Math.round(getRandomNumber(0, 10)); 

      //If the percentage is one and there are no powerUps on the stage it's okay to add a powerup. 
      if (percentage < 6 && powerUp.length == 0) { 
       var pwrp = new Powerup(this.x, this.y); 
       powerUp.push(pwrp); 
       if (!powerUpLayer) { 
        powerUpLayer = new createjs.Container(); 
        stage.addChild(powerUpLayer); 
       } 
      } 

      //Increase the score 
      score.increaseScore(this.killScore); 

      //Delete the object 
      delete this; 

      //Remove the sprite 
      enemyLayer.removeChild(this.sprite); 

      //Remove the enemy and the bullets from their array 
      enemies.splice(arrayIndex, 1); 
      for (var i in this.bullets) { 
       ammoLayer.removeChild(this.bullets[i].sprite); 
       delete this.bullets[i]; 
      } 
      this.bullets = []; 
     } else { 
      //If the enemy didnt die, update the score with the hit score. 
      score.increaseScore(this.hitScore); 
     } 

     countEnemies(); 
    } 

    this.draw = function() { 
     //enemyLayer.draw(); 
    } 

    this.move = function() { 
     this.sprite.gotoAndPlay("walk"); 
     //Set a boolean that will check later on if the enemy should change direction. Therefore getting a new X an Y. 
     var directionChange = false; 
     this.x += this.velX; 
     this.y += this.velY; 

     this.sprite.x = this.x; 
     this.sprite.y = this.y; 

     if (this.y <= -150) { 
      directionChange = true; 
     } 

     if (this.y >= (stage.canvas.height-this.sprite.height)+150) { 
      directionChange = true; 
     } 

     if (this.x <= -150) { 
      directionChange = true; 
     } 

     if (this.x >= (stage.canvas.width-this.sprite.width)+150) { 
      directionChange = true; 
     } 

     if (directionChange == true) { 
      var target = new Target(this.x, this.y); 
      this.velX = target.velX; 
      this.velY = target.velY; 
     } 
    } 

    this.flickr = function() { 
     this.sprite.alpha = 0.5; 
     var enem = this.sprite; 
     setTimeout(function() { 
      enem.alpha = 1; 
     }, 100); 
    } 

    this.bullets = []; 
} 

由於請求敵人創建功能。 (P.S階段在滾動條,其被細運行被更新)

var enemies = []; 
var newEnemiesAmount; 
var oldEnemiesAmount; 
function createEnemies(amount) { 
    oldEnemiesAmount = (amount > 0) ? amount : oldEnemiesAmount; 
    newEnemiesAmount = amount+(Math.floor(oldEnemiesAmount)/5) 

    //Create a layer to spawn the enemies on 
    enemyLayer = new createjs.Container(); 

    //Loop through the amount wanted. 
    for (var i = 0; i < newEnemiesAmount; i++) { 
     enemy = new Enemy(i); 
     createEnemyBullet(enemy); 
     //push the object in an array and add it to the newly made layer 
     enemies.push(enemy); 
     enemyLayer.addChild(enemy.sprite); 
    } 

    stage.addChild(enemyLayer); 
} 
+0

您是否看到spritesheet顯示?你沒有在你的應用程序中顯示創建敵人的代碼,添加到舞臺上的精靈,或者更新了舞臺 - 你能否展示一些上下文? – Lanny

+0

是的。我看到序列中的第一個圖像。這個項目非常龐大,老實說,小提琴根本無法選擇。我有點希望我在創建spritesheet時犯了一個愚蠢的錯誤。除了。該階段在股票代碼中更新,所以這應該不成問題。我會將敵人的創作加入我的文章。所以當你讀這篇文章的時候它應該在那裏;-) – CaptainCarl

+0

我沒有看到任何跳出來的東西 - 或許將SpriteSheet的東西隔離到一個尖峯以確保它能正常工作 - 我懷疑還有其他東西是從外部影響它。 – Lanny

回答

1

嘗試構建spritesheet時指定在spritesheet幀數。

var spriteSheet = new createjs.SpriteSheet({ 
    images: ["resources/sprites/enemy_spritesheet.png"], 
    frames: {width:40, height:40, count: 4}, 
    animations: { 
     walk: [0, 3] 
    } 
});