到达容器边框时停止移动图像 [英] stop image from moving when reaching the border of a container

查看:70
本文介绍了到达容器边框时停止移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小提琴: 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 ?

推荐答案

演示 http:// jsfiddle。 net / u6vXK / 1 /

您想要的条件是

   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;
   }

如果你按住它不会工作,按键一个接一个,意味着按并等待动画完成并再次按下,你必须添加isanimating条件和其他优势提示。

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.

这篇关于到达容器边框时停止移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆