Solved

How do I get object collision to work when using the Mouse follow Script?

Posted on 2009-05-13
3
337 Views
Last Modified: 2012-05-06
Hi guys, I'm back and still working on my breakout type game.  I have found this awesome script that let's me have my paddle move with my mouse cursor but since I added it, the object collision stopped working.  What I have figured out so far is even though the paddle image is on the screen, the paddle object is still sitting in it's original position and does not collide with the ball.  I seek you excellent advice again.  

Also on a side note, with the follow mouse script, I'm trying to follow on X axis only but when i comment out the Y axis i get multiple javascript page errors, it runs, is there a way to modify it so it only utilizes the X axis?

I have posted the code, everything I think is relevant to this issue, if you need more, just tell me and i'll post the whole thing.

Once again, thanks in advance for the help
// Sections from the rebound.js file
 
 

function init() {

    //instantiate HTML object instance vars

    ball = document.getElementById('idball');

    paddle = document.getElementById('idpaddle');

    score = document.getElementById('score');

    hiScore = document.getElementById('hiScore');

    gameTime = document.getElementById('gameTime');

    //resister key listener with document object

    document.onkeydown = keyListener;
 

//function keyListener(e) {

//    if (!e) {

//        //for IE

//        e = window.event;

//    }

//    if (e.keyCode == 37 && paddleLeft > 10) {

//        //keycode 37 is left arrow

//        paddleLeft -= 10;

//        paddle.style.left = paddleLeft + 'px';

//    }

//    if (e.keyCode == 39 && paddleLeft < 425) {

//        //keycode 39 is right arrow

//        paddleLeft += 10;

//        paddle.style.left = paddleLeft + "px";

//    }

//}
 

function detectCollisions() {

    //just reflect the ball on a collision

    //a more robust engine could change trajectory of

    //the ball based on where the ball hits the paddle

    if (collisionX())

        dx = dx * -1;

    if (collisionY())

        dy = dy * -1;

}
 

function collisionX() {

    //check left and right boundaries

    if (ballLeft < 4 || ballLeft > 480)

        return true;

    return false;

}
 

function collisionY() {

    //check if at top of playing area

    if(ballTop < 4)

    return true;

    //check to see if ball collided with paddle

    if(ballTop >450) {

        if(ballLeft > paddleLeft - 10 && ballLeft < paddleLeft + 64)

            return true;

        }

    return false;

}
 

var divName = 'mypaddle';  // div that is to follow the mouse

                           // (must be position:absolute)

var offX = -45;            // X offset from mouse position

var offY = 0;              // Y offset from mouse position
 

function mouseX(evt) {

	if (!evt) evt = window.event; 

	if (evt.pageX) return evt.pageX; 

	else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
 

//function mouseY(evt) {

	//if (!evt) evt = window.event;

	//if (evt.pageY) return evt.pageY; 

	//else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 

	//else return 0;}
 

function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style; obj.visibility = 'visible';

obj.left = (parseInt(mouseX(evt))+offX) + 'px';

obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}

document.onmousemove = follow;
 
 

**************************************************
 

//HTML Div Tag

<div id="mypaddle">

            <img src="img\paddle1.gif" id="paddle" alt="" />

        </div>
 
 

*****************************************
 

//CSS Class 

#mypaddle 

{

	position:absolute; 

	overflow:hidden; 

	visibility: hidden; 

	top: 470px;

	width:64px; 

	height:16px;

Open in new window

0
Comment
Question by:dtolle
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Graceful_Penguin earned 250 total points
ID: 24382446
The errors is caused by this line :
obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}
Whitch calls to the function you commented out. so I also commented it out.

About the object collision, I can't see how the follow script can influence it but I think you need to post code that calls the detectCollisions() function. I think the problem could be there.
// Sections from the rebound.js file

 

 

function init() {

    //instantiate HTML object instance vars

    ball = document.getElementById('idball');

    paddle = document.getElementById('idpaddle');

    score = document.getElementById('score');

    hiScore = document.getElementById('hiScore');

    gameTime = document.getElementById('gameTime');

    //resister key listener with document object

    document.onkeydown = keyListener;

 

//function keyListener(e) {

//    if (!e) {

//        //for IE

//        e = window.event;

//    }

//    if (e.keyCode == 37 && paddleLeft > 10) {

//        //keycode 37 is left arrow

//        paddleLeft -= 10;

//        paddle.style.left = paddleLeft + 'px';

//    }

//    if (e.keyCode == 39 && paddleLeft < 425) {

//        //keycode 39 is right arrow

//        paddleLeft += 10;

//        paddle.style.left = paddleLeft + "px";

//    }

//}

 

function detectCollisions() {

    //just reflect the ball on a collision

    //a more robust engine could change trajectory of

    //the ball based on where the ball hits the paddle

    if (collisionX())

        dx = dx * -1;

    if (collisionY())

        dy = dy * -1;

}

 

function collisionX() {

    //check left and right boundaries

    if (ballLeft < 4 || ballLeft > 480)

        return true;

    return false;

}

 

function collisionY() {

    //check if at top of playing area

    if(ballTop < 4)

    return true;

    //check to see if ball collided with paddle

    if(ballTop >450) {

        if(ballLeft > paddleLeft - 10 && ballLeft < paddleLeft + 64)

            return true;

        }

    return false;

}

 

var divName = 'mypaddle';  // div that is to follow the mouse

                           // (must be position:absolute)

var offX = -45;            // X offset from mouse position

var offY = 0;              // Y offset from mouse position

 

function mouseX(evt) {

	if (!evt) evt = window.event; 

	if (evt.pageX) return evt.pageX; 

	else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}

 

//function mouseY(evt) {

	//if (!evt) evt = window.event;

	//if (evt.pageY) return evt.pageY; 

	//else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 

	//else return 0;}

 

function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style; obj.visibility = 'visible';

obj.left = (parseInt(mouseX(evt))+offX) + 'px';

//obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}

document.onmousemove = follow;

 

 

**************************************************

 

//HTML Div Tag

<div id="mypaddle">

            <img src="img\paddle1.gif" id="paddle" alt="" />

        </div>

 

 

*****************************************

 

//CSS Class 

#mypaddle 

{

	position:absolute; 

	overflow:hidden; 

	visibility: hidden; 

	top: 470px;

	width:64px; 

	height:16px;

Open in new window

0
 

Author Comment

by:dtolle
ID: 24387106
I will give that a try today.
0
 

Author Comment

by:dtolle
ID: 24598104
Sorry it took so long to respond, It worked so far with what i need, thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Bootstrap 3 icons 3 40
How can I do a Select All on this page? 8 36
Making my existing website "mobile friendly"... 10 36
Split in Javascript 5 31
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now