Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
JavaScript - Detecting and displaying a message in a predetermined area.
Old 06-26-2009, 10:06 PM JavaScript - Detecting and displaying a message in a predetermined area.
Junior Talker

Posts: 1
Name: Desiree
Trades: 0
Hi I am doing an assignment that uses JavaScript and wondering if anyone had any suggestions. I am really new to this and am not very familiar with the language so here it goes:

In the assignment I am asked to create a lunar lander game from elements in the HTML document.

I have been asked to make the lunar lander move up, down, left, right. I have this working, and I also have boundaries set. But what I need is some code to check to see if the lunar lander is in a predetermined range, once it is there a message image will pop up. This is where I am having the issues.

Here is the HTML code:

<body onload="hideMessageBubble('landed')">

<div id="landscape">
<img src="assets/lunarlandscape.gif" alt="Lunar Landscape" id="landscape1"/> </div>

<div id="lander">
<img src="assets/lundar_lander.gif" alt="Lunar Lander" id="lander1" /></div>

<div id="youLanded">
<img src="assets/you_landed.gif" id="landed" /></div>

<div></div>

<div id="controlPanel">
<img src="assets/left_but.gif" alt="Left" id="left" onmousedown="moveLander('lander', 10, 'left')" />
<img src="assets/right_but.gif" alt="Right" id="right" onmousedown="moveLander('lander', 10, 'right')" />
<img src="assets/up_but.gif" alt="Up" id="up" onmousedown="moveLander('lander', 10, 'up')" />
<img src="assets/down_but.gif" alt="Down" id="down" onmousedown="moveLander('lander', 10, 'down')" /> </div>
</body>

Here is what I have so far for the JavaScript part:

// JavaScript Document
////////////////////////////////////////////////
/////Assignment 5.1 - External JavaScript///////
////////////////////////////////////////////////

var element;
//Function to hide message bubble onload.
function hideMessageBubble(id) {
element = document.getElementById(id);
element.style.visibility = "hidden";
}

//Variables and values to define position
//of element.

var minTop = 120;
var maxTop = 200;
var minLeft = 120;
var maxLeft = 475;
var currentLeft = 110;
var currentTop = 210;

//Targets created to detect when the lander comes into the area in which the message will be displayed.

var targetXSt = 320;
var targetYSt = 180;
var targetXEn = 370;
var targetYEn = 160;

//Function to define the elements position
//on click of arrow buttons.

function moveLander(id, distance, direction) {
var div = document.getElementById(id);
if(direction == "up" && currentTop >= (minTop + distance)) {
currentTop -= distance;
div.style.top = currentTop + "px";
var nowYEn = div.style.top;
} else if (direction == "down" && currentTop <= (maxTop - distance)) {
currentTop += distance;
div.style.top = currentTop + "px";
var nowYSt = div.style.top;
} else if (direction == "left" && currentLeft >= (minLeft + distance)) {
currentLeft -= distance;
div.style.left = currentLeft + "px";
} else if (direction == "right" && currentLeft <= (maxLeft + distance)) {
currentLeft += distance;
div.style.left = currentLeft + "px";

//I have this code displaying the message, but when I go to add the rest
//of the conditions, it does not seem to run.

} else if (currentTop >= (targetYEn + distance)) {// && currentTop <= (targetYSt - distance)) {
element.style.visibility = "visible";
}
}

If anyone could help me that would be great!!

Thank you!
barrelracer10 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to JavaScript - Detecting and displaying a message in a predetermined area.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10227 seconds with 12 queries