Jump to content

igkman

Members
  • Posts

    13
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

igkman's Achievements

Newbie

Newbie (1/14)

1

Reputation

  1. I have a group of Enemy(name of my object) objects that I'm trying to call a certain function from the Enemy class. When I try to do so, nothing would happen. So I wrote in an alert to see if I could access one of the variables from the Enemy class and it told me that the variable was undefined. The code is in the createEnemy class of the Game class. I'm not sure if the Enemy class would be needed or not so I added it here just to be safe. Game class: Game = function(game) {} Phaser.GameObjectFactory.prototype.enemy = function(x,y,xPix,yPix,enemyNum) { return this.game.add.existing(new Enemy(this.game,x,y,xPix,yPix,enemyNum) ); } var entranceTimer; var G1 = []; var cnt = 0; Game.prototype.preload = function() { game.load.image('galaga', './assets/images/galaga.png'); game.load.image('bullet', './assets/images/bullet.png'); game.load.spritesheet('enemy1', './assets/images/enemy1.png',30,20); game.load.spritesheet('enemy2', './assets/images/enemy2.png',30,20); game.load.spritesheet('enemy3', './assets/images/enemy3.png',36,32); game.load.spritesheet('enemy3Hit', './assets/images/enemy3Hit.png',36,20); game.load.image('explosion', './assets/images/explosion.png'); game.load.spritesheet('pixel','./assets/images/dot.png'); game.load.audio('pewpew', './assets/sounds/pewpew.wav'); } Game.prototype.create = function() { game.physics.startSystem(Phaser.Physics.ARCADE); enemies = game.add.group(); enemies.enableBody = true; // locations for enemies var group1PixelLocations = { 'x' : [game.width/2,game.width/2-30,game.width/2,game.width/2-30, game.width/2,game.width/2-30,game.width/2,game.width/2-30], 'y' : [(game.height/2)-30,(game.height/2)-30,(game.height/2)-60,(game.height/2)-60, (game.height/2)-90,(game.height/2)-90,(game.height/2)-120,(game.height/2)-120], }; //create enemies //group 1 for (var i = 0; i < 8; i = i+2) { if ( i == 0 || i == 1 || i == 2 || i == 3) { enemies.create(game.add.enemy(game.width/1.33,0,group1PixelLocations.x[i],group1PixelLocations.y[i],1)); //create another enemy object with opposite coordinates enemies.create(game.add.enemy(game.width/4,0,group1PixelLocations.x[i+1],group1PixelLocations.y[i+1],1)); } else { enemies.create(game.add.enemy(game.width/1.33,0,group1PixelLocations.x[i],group1PixelLocations.y[i],0)); //create another enemy object with opposite coordinates enemies.create(game.add.enemy(game.width/4,0,group1PixelLocations.x[i+1],group1PixelLocations.y[i+1],0)); } } this.createEnemy(); } Game.prototype.update = function() { } Game.prototype.createEnemy = function() { // supposed to store an Enemy object into the G1 array from the //enemies group enemies.forEach(function(en) { // gets xPix var from Enemy class alert(en.xPix); G1.push(en); }); } Game.prototype.shootFunction = function() { bullet.fire() } Enemy Class: the function that I was trying to call originally was group1Path, but I couldn't due to the Enemy object being 'undefined' var Enemy = function(game,x,y,xPix,yPix,enemyNum) { Phaser.Sprite.call(this,game,x,y,''); // these two variables are undefined //alert(xPix + " " + yPix); this.xPix = xPix; this.yPix = yPix; this.enemyNum = enemyNum; this.create(); } Enemy.prototype = Object.create(Phaser.Sprite.prototype); Enemy.prototype.constructor = Enemy; Enemy.prototype.preload = function() { } Enemy.prototype.create = function() { var enemySprites = ['enemy1','enemy2','enemy3']; // enemy sprite and its animation this.enemy = game.add.sprite(this.x,this.y,enemySprites[this.enemyNum]); this.enemy.anchor.set = 0.5; this.enemy.animations.add(enemySprites[this.enemyNum],null,10,true); this.enemy.animations.play(enemySprites[this.enemyNum]); //This will be tracked by the enemy sprite //will be set to invisible this.pixel = game.add.sprite(this.xPix,this.yPix,'pixel'); this.pixel.visible = false; game.physics.arcade.enable(this.enemy); game.physics.arcade.enable(this.pixel); this.sideTween(); } var completed = false; Enemy.prototype.update = function() { if (!this.exists) return; // when group1Path is completed, var compelete = true // then enemy will follow the pixel if (completed) game.physics.arcade.moveToObject(this.enemy,this.pixel,100); } Enemy.prototype.isComplete = function() { completed = true; } //path of the first group Enemy.prototype.group1Path = function() { //function } }
  2. That's what it's suppose to do. I might be misinterpreting the function though
  3. enemies.getAt(this.cnt) is supposed to return a child of the group "enemies" at the index 'this.cnt'. cnt is an integer set at zero. does that help?
  4. How is that? The problem lies within the update function of the Game class.
  5. Game = function(game) { this.timer = 0; this.cycle = 1000; this.cnt = 0; } Phaser.GameObjectFactory.prototype.enemy = function(x,y,xPix,yPix,enemyNum) { return this.game.add.existing(new Enemy(this.game,x,y,xPix,yPix,enemyNum) ); } Game.prototype = { create: function() { game.physics.startSystem(Phaser.Physics.ARCADE); enemies = game.add.group(); enemies.enableBody = true; // locations for the first group of enemies to fly in var group1PixelLocations = { 'x' : [game.width/2,game.width/2-30,game.width/2,game.width/2-30, game.width/2,game.width/2-30,game.width/2,game.width/2-30], 'y' : [(game.height/2)-30,(game.height/2)-30,(game.height/2)-60,(game.height/2)-60, (game.height/2)-90,(game.height/2)-90,(game.height/2)-120,(game.height/2)-120], }; //create enemies for (var i = 0; i < 8; i = i+2) { enemies.create(game.add.enemy(game.width/1.33,0,group1PixelLocations.x[i],group1PixelLocations.y[i],0)); //create another enemy object with opposite coordinates enemies.create(game.add.enemy(game.width/4,0,group1PixelLocations.x[i+1],group1PixelLocations.y[i+1],1)); } }, update: function() { if (game.time.now > this.timer) { this.timer = game.time.now + this.cycle; var en = enemies.getAt(this.cnt); // this is the function I want each child to perform en.group1Path(); this.cnt = this.cnt + 1; } } }; I'm trying to call a function that I created in the Enemy class(below) with the children in my group. However, the error I get is 'Uncaught TypeError: en.group1Path is not a function' . Which is odd becuase the group consists of Enemy objects. Enemy class: var Enemy = function(game,x,y,xPix,yPix,enemyNum) { Phaser.Sprite.call(this,game,x,y,''); this.xPix = xPix; this.yPix = yPix; this.enemyNum = enemyNum; this.create(); } Enemy.prototype = Object.create(Phaser.Sprite.prototype); Enemy.prototype.constructor = Enemy; var completed = false; Enemy.prototype.preload = function() { } Enemy.prototype.create = function() { //this.group1Path(); } Enemy.prototype.update = function() { if (!this.exists) return; if (completed) game.physics.arcade.moveToObject(this.enemy,this.pixel,100); } Enemy.prototype.isComplete = function() { completed = true; } //path of the first group Enemy.prototype.group1Path = function() { /*This is the function to be called*/ }
  6. var group1PixelLocations = { 'x' : [game.width/2,game.width/2-30,game.width/2,game.width/2-30,game.width/2,game.width/2-30], 'y' : [(game.height/2)-30,(game.height/2)-60,(game.height/2)-90,(game.height/2)-120,(game.height/2)-150], }; //create enemies for (var i = 0; i < 5; i++) { enemies.create(game.add.enemy(game.width/1.33,0,group1PixelLocations.x[i],group1PixelLocations.y[i])); } The last two parameters of game.add.enemy() ,xPix and yPix, are undefined when I try to pass them unto my Enemy Class: var Enemy = function(game,x,y,xPix,yPix) { Phaser.Sprite.call(this,game,x,y,''); // for some reason, xPix and yPix are undefined alert(xPix + ' ' + yPix); this.create(); } Enemy.prototype = Object.create(Phaser.Sprite.prototype); Enemy.prototype.constructor = Enemy; var completed = false; Enemy.prototype.preload = function() { } Enemy.prototype.create = function() { var enemySprites = ['enemy1','enemy2','enemy3']; // enemy sprite and its animation this.enemy = game.add.sprite(this.x,this.y,enemySprites[0]); this.enemy.anchor.set = 0.5; this.enemy.animations.add(enemySprites[0],null,10,true); this.enemy.animations.play(enemySprites[0]); //This will be tracked by the enemy sprite //will be set to invisible this.pixel = game.add.sprite(this.xPix,this.yPix,'pixel'); //this.pixel.visible = false; /*alert(this.xPix+ ' ' + this.yPix);*/ game.physics.arcade.enable(this.enemy); game.physics.arcade.enable(this.pixel); this.sideTween(); this.group1Path(); } Enemy.prototype.update = function() { if (!this.exists) return; if (completed) game.physics.arcade.moveToObject(this.enemy,this.pixel,100); } /*the path of pixel moves back and forth represents the movement of enemy sprites while enemies are flying in*/ Enemy.prototype.sideTween = function() { this.tween = game.add.tween(this.pixel).to({x: this.xPix + 400}, 2000, null,-1,true); this.tween.yoyo(true,0,0); this.tween.repeat(-1); this.tween.start(); }
  7. Can you check my code again to see if I'm still overwriting anything? I'm now getting this error: 'Uncaught TypeError: Cannot read property 'animations' of null' for my enemy1 variable Which I think is odd. It is null at first, but that should change in the create function. Is it still being overwritten? var Enemy = function(game,x,y) { Phaser.Sprite.call(this,game,x,y,'playerKey'); this.enemy1 = null; this.pixel = null; this.x = x; this.y = y; this.tween = null; this.game = game; } Enemy.prototype = Object.create(Phaser.Sprite.prototype); Enemy.prototype.constructor = Enemy; Enemy.prototype.preload = function() { game.load.spritesheet('enemy1', './assets/images/enemy1.png',30,20); game.load.image('pixel','./assets/images/dot.png'); } Enemy.prototype.create = function() { // enemy sprite and its animation this.enemy1 = game.add.sprite(this.x,this.y,'enemy1'); this.enemy1.animations.add('enemy1',null,10,true); //This will be tracked by the enemy sprite // will be set to invisible this.pixel = game.add.sprite(this.x + 200,200,'pixel'); game.physics.arcade.enable(this.enemy1); game.physics.arcade.enable(this.pixel); // the path of pixel // moves back and forth // represents the movement of enemy sprites // while enemies are flying in this.tween = game.add.tween(pixel).to({x: this.x + 400}, 2000, null,-1,true); this.tween.yoyo(true,0,0); this.tween.repeat(-1); this.update(); } Enemy.prototype.update = function() { this.enemy1.animations.play('enemy1'); game.physics.arcade.moveToObject(this.enemy1,this.pixel,100); this.tween.start(); } I really appreciate your help, by the way.
  8. I'm still getting the error. This is the peice of code that adds the Enemy object to multiple groups in a create function: enemies = game.add.group(); enemies.classType = Enemy; enemies.enableBody = true; enemies.createMultiple(10).forEach( function(sprite) { sprite.x = 30; sprite.y = 300; },this); Here's my Enemy class: var Enemy = function(game,x,y) { Phaser.Sprite.call(this,game,x,y,'playerKey'); this.enemy1 = null; this.pixel = null; this.x = x; this.y = y; this.tween = null; this.game = game; } Enemy.prototype = Object.create(Phaser.Sprite.prototype); Enemy.prototype.constructor = Enemy; Enemy.prototype = { preload: function() { game.load.spritesheet('enemy1', './assets/images/enemy1.png',30,20); game.load.image('pixel','./assets/images/dot.png'); }, create: function() { // enemy sprite and its animation enemy1 = game.add.sprite(this.x,this.y,'enemy1'); enemy1.animations.add('enemy1',null,10,true); //This will be tracked by the enemy sprite // will be set to invisible pixel = game.add.sprite(this.x + 200,200,'pixel'); game.physics.arcade.enable(enemy1); game.physics.arcade.enable(pixel); // the path of pixel // moves back and forth // represents the movement of enemy sprites // while enemies are flying in tween = game.add.tween(pixel).to({x: this.x + 400}, 2000, null,-1,true); tween.yoyo(true,0,0); tween.repeat(-1); this.update(); }, update: function() { enemy1.animations.play('enemy1'); game.physics.arcade.moveToObject(enemy1,pixel,100); tween.start(); } }
  9. I tried that with my own project and I got the following error: Uncaught TypeError: this.onTextureUpdate is not a function(…) Code: enemies.createMultiple(10).forEach( function(sprite) { sprite.game = game; sprite.x = 30; sprite.y = 300; });
  10. I would also like to know how I could set the parameters of the instances as well. function MyAwesomeSprite(game, x, y) { Phaser.Sprite.call(this, game, x, y, 'playerKey'); } // All that custom sprite class stuff... var myGroup = game.add.group(); myGroup.classType = MyAwesomeSprite; myGroup.createMultiple(20); // myGroup now has 20 instances of MyAwesomeSprite. I found this here, but I was confused as to how would one set the x and y parameters while creating 20 instances of said object?
  11. Isn't this snippet of code supposed to do that? bullet = game.add.weapon(2,'bullet');
  12. var player; var keys; var shootButton; var bullet; function create() { //game.add.sprite(0,0, 'background'); game.physics.startSystem(Phaser.Physics.ARCADE); //creates player player = game.add.sprite(0.45*600,600-50,'galaga'); game.physics.arcade.enable(player); // allows player to fire 2 bullets bullet = game.add.weapon(2,'bullet'); // when bullet leaves the screen, it will be destroyed bullet.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; // offset rotation bullet.bulletAngleOffset = 90; // prevents ship from auto firing bullet.autofire = false; // The speed at which the bullet is fired bullet.bulletSpeed = 400; //keeps track of how many bullets were shot bullet.shots = 0; // Tell the bullet to track the 'player' Sprite, offset by 16px horizontally, 0 vertically bullet.trackSprite(player, 16, 0); //enabling keyboard use keys = game.input.keyboard.createCursorKeys(); shootButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); } function update() { // Reset the players velocity (movement) player.body.velocity.x = 0; if (keys.left.isDown) { // Move to the left player.body.velocity.x = -150; } else if (keys.right.isDown) { // Move to the right player.body.velocity.x = 150; } else { //ship is idle } //shooting the bullet if (shootButton.isDown) { bullet.fire(); } } What I'm trying to do is prevent my player variable from autofiring(holding down space button to fire bullets.). I want the player to be able to shoot two bullets at a time, but only when the player presses the fire button rather than holding it. I was under the impression that bullet.autofire = false; would do the trick, but I was wrong. Am I using the autofire variable the wrong way?
×
×
  • Create New...