Skip to main content

Forums

This topic contains 2 replies, has 2 voices, and was last updated by  bearhead 3 years, 11 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #208699

    bearhead
    Participant

    I’m trying to create a game where the player creates “units” (divs) that move up the screen by pressing the spacebar. The issue I’m having is that each unit is created at the y value that the first one is currently at, instead of at the y value of the cursor.

    I know why my code is producing the result it is, but I can’t figure out how I would re-write it such that each unit is created at the cursor. It seems like each instance of unit needs to be assigned its own variable for it’s position? but I can think of how to write that…

    Here is what I have so far:
    The black outlined box is the “cursor”, you can move it left and right with arrow keys. press space to create a “unit”.

    #208703

    Shikkediel
    Participant

    This reply has been reported for inappropriate content.

    Maybe give this a go (seems to work alright) :

    $(".unit").each(function() {
    
    var current_top = $(this).position().top;
    current_top -=2; 
    $(this).offset({top:current_top});
    });
    

    Almost Space Invaders. :-D

    #208837

    bearhead
    Participant

    Thank you, that’s perfect!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star