1
I would like to move the doll and it just stay inside the div, when I hit the line that bounds the square, it would stop and I couldn’t go beyond.
$(document).ready(function(){
$(document).keydown(function(x){
if(x.which == 39 || x.keyCode == 39){
$('.pacman')
.animate({left: '1180px'}, 'slow')
.css({ transform : 'rotate(360deg)'});
}
if(x.which == 40 || x.keyCode == 40){
$('.pacman')
.animate({top: '1180px'}, 'slow')
.css({ transform : 'rotate(90deg)'});
}
if(x.which == 37 || x.keyCode == 37){
$('.pacman')
.animate({left: '-1180px'}, 'slow')
.css({ transform : 'rotate(180deg)'});
}
if(x.which == 38 || x.keyCode == 38){
$('.pacman')
.animate({top: '-1180px'}, 'slow')
.css({ transform : 'rotate(270deg)'});
}
}).keyup(function(){
$('.pacman').stop();
});
});
.pacman{
position:absolute;
}
.cenario{
border:5px solid black;
width:500px;
height:500px;
margin-left:400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cenario">
<img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTWuBaJWq9UP_ILy5Tgz5-5Z6dMm8f7URRSh8Pr2pMU0LZfvPEG" class="pacman" />
</div>
Can you place a demo on JSFIDDLE? https://jsfiddle.net/
– Not The Real Hemingway
This is Samir Braga, man. But it’s simple to understand, I move the doll with the arrow keys on the keyboard, but I would like it to stay just inside this square (div), but if you run the program you can notice that he (the doll) is coming out of the square. I wanted to delimit the area that it can go, in this case only inside the div. I do not know if I understood...
– blackbird312