I'm trying to make a shooter type game for a school project and I'm running in to trouble creating multiple divs with different classes associated with them. I'm a pretty new programmer so any help would be appreciated.
$(document).ready(function(){
var targetColor;
var randLocation;
var laserCounter = 0;
var targetCounter = 0; //creates multiple laser objects that can be accessed separately through an array
var laser = [];//making a different variable to animate lasers individually
var laserX;
var targetX;
var radians;
var randColor;
//pointer points in direction of cursor
function mouse(e){
var pointerOffset = $('.pointer').offset();
//calculates x and y offsets from the center of the div
pointer_x = (pointerOffset.left) + ($('.pointer').width()/2);
pointer_y = (pointerOffset.top) + ($('.pointer').height()/2);
var mouse_x = e.pageX;
var mouse_y = e.pageY;
radians = Math.atan2(mouse_x - pointer_x, mouse_y - pointer_y);
var degree = (radians * (180 / Math.PI) * -1) + 90;
$('.pointer').css('transform', 'rotate(' + degree + 'deg)');//transforms div certain degrees calculated based on degrees from pointer to div
};
function targetSpawn(){//spawning targets of random color
//random color picker
randColor = Math.floor((Math.random() * 4) + 1);
switch(randColor){
case 1:
targetColor = 'red';
break;
case 2:
targetColor = 'yellow';
break;
case 3:
targetColor = 'blue';
break;
case 4:
targetColor = 'green';
break;
};
randLocation = Math.floor((Math.random() * 1600) + 1);
$('.container').append('<div class="target"></div>'); //adds a div
$('.target').offset({top: -100, left: randLocation});
$('.target').css('background-color', targetColor);
$('.target').animate({top: "+800px"}, 10000);
};
$(document).keypress(function(){
$('.container').append('<div class="laser" + laserCounter></div>'); //adds a div
$('.laser').offset({top: pointer_y, left: pointer_x});//starts the laser at the pointer
x = (100 / Math.tan(radians));
$('.laser').animate({left: "+" + x + "px", top: "+100px"}, 1000);//moves the lasers to that angle
$('.score').append(laserCounter);
laserCounter++;//changes the next div that will be added
});
$(document).mousemove(mouse);//does function point the pointer
$(document).click(targetSpawn);//for testing the random div spawn
});
I am planning on only using HTML CSS JS and jQuery.