2

i have this fiddle : http://jsfiddle.net/ps4t9/4/

$(window).keydown(function (e) {
        if (e.which == 38) { player.animate({ top: '-=20px' }); shuriken.animate({ top: '-=20px' }); } // up
        if (e.which == 40) { player.animate({ top: '+=20px' }); shuriken.animate({ top: '+=20px' }); } // down
        if (e.which == 32) { shuriken.animate({ left: '+=280px' });} // space bar hit

    });

how can i prevent the player from moving outside the border of the container ?

Sora
  • 2,697
  • 15
  • 62
  • 127

6 Answers6

1

Try this: http://jsfiddle.net/ps4t9/11/

$(document).ready(function () {
  var player = $("#Player"),
      shuriken = $("#Shuriken"),
      container = $("#Container"),
      w = container.width() - shuriken.width();

  $(window).keydown(function (e) {
    if (e.which == 38) {
      if (parseInt(player.css('top')) > 10) {
        player.animate({ top: '-=20px' });
        shuriken.animate({ top: '-=20px' });
      }
    } // up
    if (e.which == 40) {
      if (parseInt(player.css('top')) < 270) {
        player.animate({ top: '+=20px' });
        shuriken.animate({ top: '+=20px' });
      }
    } // down
    if (e.which == 32) { 
      if (shuriken.css('left') != '249px') {
        shuriken.animate({ 'left' : '+=280px' });
      }
    }
  });
});

It breaks when holding down the key and moving too fast though. You may also have to adjust the values a bit.

Richard
  • 3,509
  • 5
  • 25
  • 33
  • but that now what i want exactly my question is when i move the player up and down if he reach the border i want to stop him – Sora Jul 20 '13 at 07:30
  • didn't work as you can see the player is still crossing the border of the container onkeydown :/ – Sora Jul 20 '13 at 08:12
1

You can use an if statement

if (e.which == 32){ 
if(shuriken.css('left') != '249px'){
shuriken.animate({ 'left' : '+=280px' });
}
}

Fiddle: http://jsfiddle.net/Hive7/ps4t9/5/

1

Demo http://jsfiddle.net/u6vXK/1/

the condition what you want is

   var wallT = 0,//top
       wallB =269,//bottpm
       wallL = 0,//left
       wallR =269;//right
  function checkBoundUpDw(pos) {                
              if(pos > wallT && pos < wallB){
               return true;
              }
              return false;
   }
 function checkBoundleftRight(pos) {
              if(pos > wallL && pos <wallR){
               return true;
              }
              return false;
   }

If you press hold it wont work , pres key one by one , means press and wait for animation finish and press again , you have to add isanimating condition and other predominance tips.

Sarath
  • 8,472
  • 11
  • 44
  • 75
0

here is the link for your answer

http://jsfiddle.net/bDMnX/7/

    var pane = $('#pane'),
    box = $('#box'),
    w = pane.width() - box.width(),
    d = {},
    x = 3;

function newv(v,a,b) {
    var n = parseInt(v, 10) - (d[a] ? x : 0) + (d[b] ? x : 0);
    return n < 0 ? 0 : n > w ? w : n;
}

$(window).keydown(function(e) { d[e.which] = true; });
$(window).keyup(function(e) { d[e.which] = false; });

setInterval(function() {
    box.css({
        left: function(i,v) { return newv(v, 37, 39); },
        top: function(i,v) { return newv(v, 38, 40); }
    });
}, 20);
Hushme
  • 2,998
  • 1
  • 16
  • 25
0

thank you all for your help but after a deep Meditation and searching hehe i managed to make it work here is the final jsfiddle : http://jsfiddle.net/ps4t9/15/ thank you

 function newv(v, a, b) {
            var n = parseInt(v, 10) - (d[a] ? x : 0) + (d[b] ? x : 0);

            return n < 0 ? 0 : n > w ? w : n;
        }



        $(window).keydown(function (e) {
            d[e.which] = true;
            if (e.which == 32) {

                if (!shurikenHitBorder) {
                    shuriken.animate({ left: '+=280px' }, 'fast');

                    shuriken.fadeIn(100);
                }
                shurikenHitBorder = true;
            }


          });
        $(window).keyup(function (e) { d[e.which] = false; });

        setInterval(function () {
            box.css({top: function (i, v) { return newv(v, 38, 40); }});
        }, 20);
Sora
  • 2,697
  • 15
  • 62
  • 127
-1

I have edited your JSFiddle with the necessary modifications. Does this help you? Ask me if you need more explanations...

I haved added these to help you calculate optimum bounds...

posDelta = 20, // amount of change in position
playerOffset = player.height() * 0.5,
playerPos = player.position().top,
maxTop = posDelta,
maxBottom = container.height() - (posDelta + playerOffset);
Van Dame
  • 150
  • 4
  • 12