lluiscab

Another collision problem ;)

Recommended Posts

I've been looking a ton of posts on this forums and also some tutorials but I can't make the collision between the bullets and the enemies work. Any help?

 

var Level1 = function(game) {};

Level1.prototype = {
	
	preload: function () {
		this.optionCount = 1;
		
		this.player;
		this.weapon;
		this.cursors;
		this.fireButton;
		this.enemies;
		
	},
	create: function () {
		
		var self = this;
		
		this.stage.disableVisibilityChange = false;

		game.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})
		
		self.weapon = game.add.weapon(30, 'weapon');
		self.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
		self.weapon.bulletAngleOffset = 90;
		self.weapon.bulletSpeed = 400;
		self.weapon.fireRate = 200;
		self.weapon.fireAngle = Phaser.ANGLE_RIGHT;
		
		self.player = this.add.sprite(50, 300, 'user');
		self.player.angle = 90;
		self.player.alpha = 0;
		
		game.add.tween(self.player).to({ alpha: 1 }, 1300, Phaser.Easing.Linear.None, true);
		
		self.game.physics.arcade.enable(self.player);
		self.weapon.trackSprite(self.player, 0, 14);
		
		self.cursors = this.input.keyboard.createCursorKeys();
		self.fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);	
		
		
		self.enemies = game.add.group();
		self.enemies.enableBody = true;
		self.enemies.physicsBodyType = Phaser.Physics.ARCADE;
		self.enemies.createMultiple(2, 'enemy1');
		self.enemies.setAll('anchor.x', 0.5);
		self.enemies.setAll('anchor.y', 0.5);
		self.enemies.setAll('scale.x', 0.5);
		self.enemies.setAll('scale.y', 0.5);
		self.enemies.setAll('alpha', 0)
		self.enemies.setAll('outOfBoundsKill', true);
		self.enemies.setAll('checkWorldBounds', true);	
		
		var enemy1 = self.enemies.getFirstExists(false);
		if (enemy1) {
			enemy1.reset(700, 150);
			game.add.tween(enemy1).to({ alpha: 1 }, 1300, Phaser.Easing.Linear.None, true);
		}
		
		var enemy2 = self.enemies.getFirstExists(false);
		if (enemy2) {
			enemy2.reset(700, 450);
			game.add.tween(enemy2).to({ alpha: 1 }, 1300, Phaser.Easing.Linear.None, true);
		}
		
		game.physics.arcade.overlap(self.weapon.bullets, self.enemies, self.hitEnemy, null, this);
	},
	update: function() {
		
		var self = this;
		
		if (self.fireButton.isDown){
			self.weapon.fire();
		}
		
		self.player.body.velocity.y = 0;
		
		if (self.cursors.up.isDown) {
			self.player.body.velocity.y = -200
		} else if (self.cursors.down.isDown) {
			self.player.body.velocity.y = 200;
		}
		
		//  Stop at screen edges
		if (self.player.y > game.height - 50) {
			self.player.y = game.height - 50;
			self.player.body.acceleration.y = 0;
		}
		if (self.player.y < 50) {
			self.player.y = 50;
			self.player.body.acceleration.y = 0;
		}
		
	},
	hitEnemy: function(enemy, bullet) {
		console.log('hey');
	}
};

 

Share this post


Link to post
Share on other sites

Thanks for the help with the overlap checking. I was sure it was just like an "init" function so you call it and then the game knows it has to check that every time, but It's just a standard function that needs to be called when I want to check.

Thanks for the help ;)

Share this post


Link to post
Share on other sites
Quote

As an aside, why are you using the self keyword? Is there a particular reason for doing this?

Hi no problem its something I was also stuck with. again I never seen anyone use self. Is there any reason for it? Especially:

var self = this;

Why not just stick to this?

Share this post


Link to post
Share on other sites
var Obj= {
 foo: "bar",
 log: function() {
    var self = this;
    console.log("external function:  this.foo = " + this.foo);
    console.log("external function:  self.foo = " + self.foo);
    console.log(this.foo === self.food);
    (function() {
        console.log("internal function:  this.foo = " + this.foo);
        console.log("internal function:  self.foo = " + self.foo);
        console.log(this.foo === self.food);
    }());
  }
};

Obj.log();

https://jsfiddle.net/caxjvLL0/

 

Well Initially I wrote a longer post but I kinda deleted it when I wrote the code (Ooops!).

But the shorter version is that, "self" is mostly used by typescript enthusiasts to keep the scope of the variables in check. Nowadays "self" is also a global keyword like "this" so he shouldn't really be using it like this anymore, since it gives me headaches ( :sad:) 

https://developer.mozilla.org/en-US/docs/Web/API/Window/self

http://stackoverflow.com/questions/962033/what-underlies-this-javascript-idiom-var-self-this

Share this post


Link to post
Share on other sites

Hmm, its still kind of strange to me, because of this for example will give the same results:

var Obj= {
 foo: "bar",
 log: function() {
    var selfie = this;
    console.log("external function:  this.foo = " + this.foo);
    console.log("external function:  selfie.foo = " + selfie.foo);
    console.log(this.foo === selfie.food);
    (function() {
        console.log("internal function:  this.foo = " + this.foo);
        console.log("internal function:  selfie.foo = " + selfie.foo);
        console.log(this.foo === selfie.food);
    }());
  }
};

Obj.log();

https://jsfiddle.net/samid737/umnngpxs/1/

You could use any naming right? But it does have some special feature in the web API when looking at your first link.

Share this post


Link to post
Share on other sites

Yes you can use any naming, the way he uses it holds no special meaning, he could have written it as anything and it would made no difference in his current code.

var self = this;

var this = this;

let self = this;

var sf = this;

var thiz = this;

var p = this;

But ! "self" is a global operand, so it's scope is pretty much like "this" noadays(and no they are not the same thing but very similar in some aspects), so he should change it to something else such as "-this" and not "_this" since "_" is also used sometimes to declare variables that are in the global scope.

 

Now this is not to say that his code won't work this is more of a cross-language thing. So even if his code works in Javascript, if someone with pure Python programming comes along to learn javascript and see's his code, that person will have a brain meltdown trying to figure it out.

Share this post


Link to post
Share on other sites

So I'm gonna disseminate his code a bit , since his question has been answered anyway :)

var self = this;
		
this.stage.disableVisibilityChange = false;

game.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})

Now in his current scope "this" refers to the "game" object so what happens here is

var self = this;

"self" becomes a copy of "this" so in essence it works as a pull request to work on local content. This is good in cases where you don't wish to alter the values in the original, but you want to modify them in your current instance and then dispose of them.

this.stage.disableVisibilityChange = false;

Using "this" here, modifies the original reference permanently. So in all future uses across the application from now on this value will persist unless changed "game.stage.disableVisibilityChange = false;"

game.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})

Could have been written as 

this.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})

And it will have the same effect of permanently altering the "game" object".

 

self.weapon = game.add.weapon(30, 'weapon');
self.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
self.weapon.bulletAngleOffset = 90;
self.weapon.bulletSpeed = 400;
self.weapon.fireRate = 200;
self.weapon.fireAngle = Phaser.ANGLE_RIGHT;

can be written like so

self.weapon = this.add.weapon(30, 'weapon');
self.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
self.weapon.bulletAngleOffset = 90;
self.weapon.bulletSpeed = 400;
self.weapon.fireRate = 200;
self.weapon.fireAngle = Phaser.ANGLE_RIGHT;

So from this point onward in the current "game state"  a weapon is added to the global view, so

game.add.weapon(30, 'weapon');

Is again a permanent addition to the game object. BUT ! Everything else is not permanent.

So all the configs for self.weapon are only available in this "game state" (this is not a real game state but I have no better way of referring to it) and should the state change, the configs will be lost.

 

self.player = this.add.sprite(50, 300, 'user');
self.player.angle = 90;
self.player.alpha = 0;

So here again.

A sprite is added with "this" to the "game" object. The sprite will be available globally from the "game" object but if refered to from outside the "scope" of the "game state" then the configs for it will be reset. So if you try and access the "player" sprite it will not have "alpha = 0" and "angle = 90"

 

 

 

So basically after he changes the "game state" the only changes left will be 

this.stage.disableVisibilityChange = false;

game.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})
		
game.add.weapon(30, 'weapon');

this.add.sprite(50, 300, 'user');
	
game.add.tween(self.player).to({ alpha: 1 }, 1300, Phaser.Easing.Linear.None, true);

this.input.keyboard.createCursorKeys();
this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);


or 

this.stage.disableVisibilityChange = false;

this.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})
		
this.add.weapon(30, 'weapon');

this.add.sprite(50, 300, 'user');
	
this.add.tween(self.player).to({ alpha: 1 }, 1300, Phaser.Easing.Linear.None, true);

this.input.keyboard.createCursorKeys();
this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);

or 

game.stage.disableVisibilityChange = false;

game.add.text(20, 20, 'Level 1', { font: "20px Arial", fill: '#ffffff'})
		
game.add.weapon(30, 'weapon');

game.add.sprite(50, 300, 'user');
	
game.add.tween(self.player).to({ alpha: 1 }, 1300, Phaser.Easing.Linear.None, true);

game.input.keyboard.createCursorKeys();
game.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);

 

In essence freeing the resources that were in use by "self" and whatever config/assets it contained exclusively.

Share this post


Link to post
Share on other sites
On 4/15/2017 at 1:19 PM, samid737 said:

Hi no problem its something I was also stuck with. again I never seen anyone use self. Is there any reason for it? Especially:


var self = this;

Why not just stick to this?

As @symof said, It's just to keep the scope saved on a copy. I normally use this when I code some class based app in nodejs

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.