1 decade ago
by bram
Hello Everyone,
I'm trying out some new things that will hopefully lead to a new game. I saw a tutorial on shooting where the users cursor is. I tried to implement it but I cant get it to work, when I run my game I get the following:
http://imgur.com/DAhfzip ( sorry it is not the best quality. ) You can check out my player.js here:
http://pastebin.com/Db533e4N
If someone could help me with this that would be great!
Thanks in advance!
1 decade ago
by Joncom
When spawning a bullet, it doesn't look like you're providing an angle. Thus, the bullet will always move in the default direction.
1 decade ago
by bram
Thank you for your answer. I've been playing around with that for a while but I can't get it to work I will get this error:
http://i.imgur.com/425Gpuq.png
which are these 2 lines:
var vely = Math.sin(this.10) * this.desiredVel;
var velx = Math.cos(this.10) * this.desiredVel;
Hey Bram,
a while ago i had a small game with that problem.
First of all you need to tell your EntityBullet where you clicked:
//player.js
if( ig.input.pressed('shoot1') ) {
ig.game.spawnEntity( EntityProjectile,
this.pos.x, this.pos.y,
{mouse: ig.input.mouse } );
}
Now we can access the mouse coords in the constructor. In my case the player is always in the middle of the screen, so i just need to set the click position in relation to the player
init: function( x, y, settings ) {
var mx = settings.mouse.x - ig.system.width/2;
var my = settings.mouse.y - ig.system.height/2;
},
Now its simple Math, we need the direction vector, so we need the length and divide it with every point to get a vector with the length = 1 with the same direction. then we can set the velocity to a defined speed
var vLength = Math.sqrt( Math.pow(mx ,2) + Math.pow(my ,2) );
this.vel.x = (mx / vLength) * this.speed;
this.vel.y = (my / vLength) * this.speed;
Here is the complete code for my projectile:
http://pastebin.com/eJTUEVYN
1 decade ago
by Joncom
Quote from bram
which are these 2 lines:
##
var vely = Math.sin(this.10) * this.desiredVel;
var velx = Math.cos(this.10) * this.desiredVel;
##
Hey bram.
Property names cannot start with a number.
So
this.10
is not legal.
Did you mean just
10
?
1 decade ago
by bram
Thank you all for your help! I got it working! Not perfect but ill figure it out!
Thanks!
Bram
Page 1 of 1
« first
« previous
next ›
last »