2012-10-31 3 views
0

Ctrl을 눌러 무작위로 점프하는 것을 막는 작은 게임을 만들고 있습니다. 그러나 나는 그것을 작동시킬 수 없다. 안에 randomJump(){return false;};을 넣을 때까지 jumpRandom 함수가 잘 동작한다. 작동 시키려면 어떻게해야합니까?Ctrl 키를 누를 때 정지 기능이 적용됩니다.

JS :

$(document).ready(function() { 

function randomFromTo(from, to){ 
      return Math.floor(Math.random() * (to - from + 1) + from); 
} 
$("#goal").bind('mouseenter keypress', function(event) { 
    if (event.ctrlKey) { 
      randomJump(){return false;}; 
     } 
}); 
$('#goal').mouseenter(randomJump); 

function randomJump(){ 
      /* get Window position and size 
      * -- access method : cPos.top and cPos.left*/ 
      var cPos = $('#pageWrap').offset(); 
      var cHeight = $(window).height() - $('header').height() - $('footer').height(); 
      var cWidth = $(window).width(); 

      // get box padding (assume all padding have same value) 
      var pad = parseInt($('#goal').css('padding-top').replace('px', '')); 

      // get movable box size 
      var bHeight = $('#goal').height(); 
      var bWidth = $('#goal').width(); 

      // set maximum position 
      maxY = cPos.top + cHeight - bHeight - pad; 
      maxX = cPos.left + cWidth - bWidth - pad; 

      // set minimum position 
      minY = cPos.top + pad; 
      minX = cPos.left + pad; 

      // set new position   
      newY = randomFromTo(minY, maxY); 
      newX = randomFromTo(minX, maxX); 


      $('#goal').fadeOut(50, function(){ 

       $('#goal').fadeIn(700); 
      }); 
      $('#goal').animate({ 
        left: newX, 
        top: newY, 
        duration: 500 
       }); 
     } 
}); 

답변

2

이 시도 :

$("#goal").bind('mouseenter keypress', function (e) { 
    randomJump(e); 
}); 

function randomJump(e) { 
    if (!e.ctrlKey) { 
     //do normal stuff 
    } else { 
     //depending on how permanent you need this to be... 
     //$("#goal").unbind('mouseenter keypress'); 
    } 
    return !e.ctrlKey; 
} 
+0

이 일 쿨! ctrl을 해제하면 어떻게'jumpRandom'을 다시 사용할 수 있습니까? –

+0

'$ ("# goal")의 주석 처리를 해제하면 unbind ('mouseenter keypress');, ** re ** - 주석 처리하십시오. :) 나는 그것이 당신이 그것을 영원히 원한다면 나는 몰랐기 때문에 그것을 주석으로 남겼다. 여기 그것을 시연하는 바이올린이 있습니다. http://jsfiddle.net/QYsDN/ – pete

+0

아, 분명히!^___ ^. 고마워! –

관련 문제