Reputation: 151
Right now. I have it setup to shoot bullets from the direction of my character. But I want to be able to shoot bullets to the mouse point, making it easier on the player.
Right now it's
if(gun_1[i].direction == 2){ gun_1[i].x -= gun_1[i].speed * modifier};
if(gun_1[i].direction == 3){ gun_1[i].x += gun_1[i].speed * modifier};
if(gun_1[i].direction == 1){ gun_1[i].y -= gun_1[i].speed * modifier};
if(gun_1[i].direction == 4){ gun_1[i].y += gun_1[i].speed * modifier };
if(gun_1[i].direction == 5){ gun_1[i].y -= gun_1[i].speed * modifier; gun_1[i].x -= gun_1[i].speed * modifier };
if(gun_1[i].direction == 7){ gun_1[i].y += gun_1[i].speed * modifier; gun_1[i].x -= gun_1[i].speed * modifier };
if(gun_1[i].direction == 6){ gun_1[i].y -= gun_1[i].speed * modifier; gun_1[i].x += gun_1[i].speed * modifier };
if(gun_1[i].direction == 8){ gun_1[i].y += gun_1[i].speed * modifier; gun_1[i].x += gun_1[i].speed * modifier };
I want to be able to shoot to location of where the mouse was clicked. If it is possible.
Upvotes: 2
Views: 3960
Reputation: 1
var i=0;
var maxBullets=10;
var allBullets=[];//a array for objects
function bullet(){
this.vx=0;
this.vy=0;
this.inix=0;
this.iniy=0;
this.angleGrads=0;
this.angleRads=1.0;
this.active=false;
}
bullet.prototype.exist=function(){
//this.inix+=mathsin.bla,bla.bla.bla bla
if(this.x > wordWidth){
//kill this bullet
this.active=false;
}
}
bullet.prototype.draw=function(){
//ctx.draw bla, bla, bla
}
function newBullets(){
for(i=1;i<=maxBullets;i++){
allBullets[i]=new bullet();
}
}
function launchBullets(){
for(i=1;i<=maxBullets;i++){
if(!allBullets[i].active){
//detemine angle with a point an the mouse point
// math atan2() ;)
//asing values to this bullet
allBullets[i].angleGrads=angleMouse;
allBullets[i].inix=mousex;
allBullets[i].iniy=mousey;
allBullets[i].angleRads=(allBullets[i].angleGrads*PI)/180;
//and end
allBullets[i].active=true;
//get out
break;
}
}
}//end of launchBullets
function operations(){
for(i=1;i<=maxBullets;i++){
if(allBullets[i].active){
allBullets[i].exist();
}
}
}
function paint(){
for(i=1;i<=maxBullets;i++){
if(allBullets[i].active){
allBullets[i].draw();
}
}
}//end of draw scene
Upvotes: 0
Reputation:
Another answer:
gun_1[i].x += gun_1[i].velocityX;
gun_1[i].y += gun_1[i].velocityY;
var dx = (e.x - character.x);
var dy = (e.y - character.y);
var angle = Math.atan2(dy, dx);
gun_1[i].velocityX = Math.cos(angle) * speed;
gun_1[i].velocityY = Math.sin(angle) * speed;
Upvotes: 2
Reputation: 6230
Sure, that's not too hard. But there is also a lot you can do to improve your current design. First of all, add velocityX
and velocityY
fields so that on each step you just need to update the bullets' positions:
gun_1[i].x += gun_1[i].velocityX
gun_1[i].y += gun_1[i].velocityY
Then when the mouse is pressed, set the bullets' velocities:
canvas.onmousedown = function(e) {
var dx = (e.x - character.x);
var dy = (e.y - character.y);
var mag = Math.sqrt(dx * dx + dy * dy);
// whatever you need to do to get gun_1[i]
gun_1[i].velocityX = (dx / mag) * speed;
gun_1[i].velocityY = (dy / mag) * speed;
}
If you know a thing or two about vectors we're just normalizing the direction vector and multiplying with the scalar initial speed.
Upvotes: 5