Move A Object by using arrow keys (java script)

Hi,


We can develop games by using java script, for I have done following code for move object by using arrow keys,  copy below code and save as html then  u can see result, enjoy it dudes :)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Try Move Objects</title>
<script src="http://code.jquery.com/jquery-1.4.3.min.js" type="text/javascript"></script>
<!--jquery code here-->
<script type="text/javascript">
jQuery(function () {
mywidth=$(window).width()-105;
myheight=$(window).height()-105;
$(document).keydown(function(event) {
mypos=$("#obj").offset();
if(mypos.left!=0){
if(event.keyCode == "37" || event.charCode == "37"){
$("#obj").animate({"left":"-=5px"},1);
}
}
if(mypos.left<=mywidth){
if(event.keyCode == "39" || event.charCode == "39"){
$("#obj").animate({"left":"+=5px"},1);
}
}
if(mypos.top!=0){
if(event.keyCode == "38" || event.charCode == "38"){
$("#obj").animate({"top":"-=5px"},1);
}}
if(mypos.top<=myheight){
if(event.keyCode == "40" || event.charCode == "40"){
$("#obj").animate({"top":"+=5px"},1);
}}
});
});
</script>
<!--jquery code end here-->
<style type="text/css">
body{margin:0px;}
#obj{width:100px; height:100px; background-color:#666666; position:absolute; top:0px; left:0px;}
</style>
</style>
</head>

<body>
<div id="obj"></div>
</body>
</html>



thanks,
K Swamy Vishnubhatla,
www.flashachievers.com.

No comments:

Post a Comment

Thanks for comment, We are professionals in Web Designing, Flash, E-learning, Logos Designing, Templates Making, Business Cards, Flash Banners, Flash Components

If you need any of assistance, please contact to swamy.webdesigner@gmail.com

Note: Only a member of this blog may post a comment.