Seeking Game Making Help (html5 / Javascript) - How to assign bullets etc to individual enemies

in javascript •  7 years ago 

Hi

I am making a game using html and javascript and I have enemies spawning and moving around in random directions and changing directions and I can get them to create bullets through my function for enemy shooting because I can see them in the console, but I am having trouble setting the bullets to begin at the right positions (the enemy x and y points plus a little). I have done this with the my player but I am having issues doing it with the enemies.

I think it might be because the enemies themselves are part of an array and the bullets are an array, so I'm applying the wrong things here, but I think there might be slightly more wrong still. They don't draw and in the console for x and y say "NaN". However something else is also up because if I delete all of the aspects of the I.y= and I.x = parts that rely on the player position and just put numbers, the Y position works out but the X position still says NaN even though all I have in the code is:

I.x=(number)

The code I have been trying for is along the lines of

I.y=newenemy.y+55 for the y

and

if (newenemy.newenemyrightEnabled)
{
I.xVelocity=I.speed;
newenemy.newenemyleftEnabled=false;
I.x=newenemy.x+102
}

for the x with a similar but opposite bit of code for if left is enabled instead.

That should make them shoot in the direction they are facing and put the bullets in an appropriate position for the direction they are facing, if it was working.

Does anyone know how I go about making sure the bullets correspond with the enemies who shot them and get them to actually draw on the screen?

I have filled in a JSfiddle with just the code relevant to this: https://jsfiddle.net/Lypzdr70/

Thank you for your help

Regards
Birchmark

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!