Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2012, 03:39 AM   PM User | #1
Mcwatson
New to the CF scene

 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Mcwatson is an unknown quantity at this point
Help with newb error

I just started with javascript and am working on a little game to wrap my head around it and came across an error I can't seem to fix.
Code:
 if (AsteriodXpos[i] < -50 || AsteriodYpos[i] < -100 || AsteriodYpos[i] > WinHeight + 100) {

                    AsteriodXpos[i] = WinWidth;

                    AsteriodYpos[i] = Math.round(Math.random() * WinHeight);

                    AsteriodXSpeed[i] = Math.random() * 5 + 7;

                    AsteriodYSpeed[i] = Math.random() * 7 - 5;
                    AstriodsAvoided += 1;
                    if (AstriodsAvoided % 15 == 0) {
                        document.write('<div style="position:absolute;top:0px;left:0px">');

                        document.write('<div style="position:relative">');
                        document.write('<div id="ast" style="position:absolute;font-size:2px"><img src="asteroid.png" height=50 width=50></div>');
                        document.write('</div>');
                        document.write('</div>');

                        AsteriodYpos[AsteriodYpos.length] = Math.round(Math.random() * WinHeight);
                        AsteriodXpos[AsteriodXpos.length] = Math.round(Math.random() * WinWidth) + WinWidth;
                        AsteriodXSpeed[AsteriodXSpeed.length] = Math.random() * 10 + 5;
                        AsteriodYSpeed[AsteriodYSpeed.length] = Math.random() * 7 - 5;
                        asteriods += 1;
                    }
                }
                ast[i].style.pixelLeft = AsteriodXpos[i];

                ast[i].style.pixelTop = AsteriodYpos[i]+hscrll;
When it seems to not like these last two lines as it says "Error: Unable to get value of the property 'style': object is null or undefined". Essentially it is a game where you are flying through space avoiding astroids. after you avoid so many it increases the number of astroids which is where the problem is happening. In case someone would like to see this in context of what im doing the whole code is
Code:
<html>
<head>
</head>
<body bgcolor="#000000" onload="fly()" onkeypress="displayunicode(event)" style="overflow: hidden">
    >
    <script language="JavaScript">



<!-- Begin
        x = 0;

        var startTime = new Date();
        // create object
        imageObj = new Image();

        // set image list
        images = new Array();
        images[0] = "ship1.png"
        images[1] = "ship2.png"
        images[2] = "ship3.png"
        images[3] = "ship4.png";
        images[4] = "ship5.png"
        images[5] = "ship6.png"

        // start preloading
        for (i = 0; i <= 5; i++) {
            imageObj.src = images[i];
        }
        AstriodsAvoided = 1;
        wasCollision = 0;
        SmallStars = 20;

        LargeStars = 50;
        asteriods = 5;
        SmallYpos = new Array();

        SmallXpos = new Array();

        LargeYpos = new Array();

        LargeXpos = new Array();

        Smallspeed = new Array();

        Largespeed = new Array();

        AsteriodXpos = new Array();
        AsteriodYpos = new Array();
        AsteriodXSpeed = new Array();
        AsteriodYSpeed = new Array();

        document.write('<div style="position:absolute;top:0px;left:0px">');

        document.write('<div style="position:relative">');


        for (i = 0; i < SmallStars; i++) {

            document.write('<div id="si" style="position:absolute;top:0;left:0;width:1px;height:1px;background:#fffff0;font-size:1px"></div>');

        }

        document.write('</div>');

        document.write('</div>');


        document.write('<div style="position:absolute;top:0px;left:0px">');

        document.write('<div style="position:relative">');


        for (i = 0; i < LargeStars; i++) {

            document.write('<div id="li" style="position:absolute;top:0;left:0;width:3px;height:2px;background:#ffffff;font-size:2px"></div>');

        }

        document.write('</div>');

        document.write('</div>');


        document.write('<div style="position:absolute;top:0px;left:0px">');

        document.write('<div style="position:relative">');

        for (i = 0; i < asteriods; i++) {

            document.write('<div id="ast" style="position:absolute;top:0;left:0;font-size:2px"><img src="asteroid.png" height=50 width=50></div>');

        }

        document.write('</div>');

        document.write('</div>');


        imgWidth = 150;
        imgHeight = 150;
        document.write('<div style="position:absolute;top:0px;left:0px">');

        document.write('<div style="position:relative">');

        document.write('<div id="shipLayer" style="position:absolute;top:0;font-size:2px"><img id="ship" src="ship.png" height=150 width=150></div>');

        document.write('</div>');

        document.write('</div>');


        WinHeight = window.document.body.clientHeight;

        WinWidth = window.document.body.clientWidth;


        ShipYpos = Math.round(Math.random() * WinHeight);
        ShipXpos = Math.round(Math.random() * WinWidth) / 2;

        ShipXSpeed = 0;
        ShipYSpeed = 0;

        shipLayer.style.pixelLeft = ShipXpos;

        shipLayer.style.pixelTop = ShipYpos;

        for (i = 0; i < SmallStars; i++) {

            SmallYpos[i] = Math.round(Math.random() * WinHeight);

            SmallXpos[i] = Math.round(Math.random() * WinWidth);

            Smallspeed[i] = Math.random() * 5 + 1;

        }


        for (i = 0; i < LargeStars; i++) {

            LargeYpos[i] = Math.round(Math.random() * WinHeight);

            LargeXpos[i] = Math.round(Math.random() * WinWidth);

            Largespeed[i] = Math.random() * 10 + 5;

        }


        for (i = 0; i < asteriods; i++) {

            AsteriodYpos[i] = Math.round(Math.random() * WinHeight);

            AsteriodXpos[i] = Math.round(Math.random() * WinWidth) + WinWidth;

            AsteriodXSpeed[i] = Math.random() * 10 + 5;

            AsteriodYSpeed[i] = Math.random() * 7 - 5;
        }





        function fly() {
            document.getElementById("ship").src = images[x];
            x += 1;
            if (x == 6) { x = 0; }
            var WinHeight = window.document.body.clientHeight;

            var WinWidth = window.document.body.clientWidth;

            var hscrll = document.body.scrollTop;

            var wscrll = document.body.scrollLeft;


            for (i = 0; i < LargeStars; i++) {

                LargeXpos[i] -= Largespeed[i];

                if (LargeXpos[i] < -10) {

                    LargeXpos[i] = WinWidth;

                    LargeYpos[i] = Math.round(Math.random() * WinHeight);

                    Largespeed[i] = Math.random() * 10 + 5;
                }


                li[i].style.pixelLeft = LargeXpos[i];

                li[i].style.pixelTop = LargeYpos[i] + hscrll;

            }

            for (i = 0; i < SmallStars; i++) {

                SmallXpos[i] -= Smallspeed[i];


                if (SmallXpos[i] < -10) {

                    SmallXpos[i] = WinWidth;

                    SmallYpos[i] = Math.round(Math.random() * WinHeight);

                    Smallspeed[i] = Math.random() * 5 + 1;

                }


                si[i].style.pixelLeft = SmallXpos[i];

                si[i].style.pixelTop = SmallYpos[i] + hscrll;

            }


            for (i = 0; i < asteriods; i++) {

                AsteriodXpos[i] -= AsteriodXSpeed[i];

                AsteriodYpos[i] -= AsteriodYSpeed[i];
                //for (j=0; j<30; j++){
                //	xDistance = AsteriodXpos[i]+25-AsteriodXpos[j]+25;
                //	yDistance = AsteriodYpos[i]-25-AsteriodYpos[j]-25;
                //	distance =xDistance*xDistance+yDistance*yDistance;
                //	if (distance<3000 && i!=j){



                //	}
                if (AsteriodXpos[i] < -50 || AsteriodYpos[i] < -100 || AsteriodYpos[i] > WinHeight + 100) {

                    AsteriodXpos[i] = WinWidth;

                    AsteriodYpos[i] = Math.round(Math.random() * WinHeight);

                    AsteriodXSpeed[i] = Math.random() * 5 + 7;

                    AsteriodYSpeed[i] = Math.random() * 7 - 5;
                    AstriodsAvoided += 1;
                    if (AstriodsAvoided % 15 == 0) {
                        document.write('<div style="position:absolute;top:0px;left:0px">');

                        document.write('<div style="position:relative">');
                        document.write('<div id="ast" style="position:absolute;font-size:2px"><img src="asteroid.png" height=50 width=50></div>');
                        document.write('</div>');
                        document.write('</div>');

                        AsteriodYpos[AsteriodYpos.length] = Math.round(Math.random() * WinHeight);
                        AsteriodXpos[AsteriodXpos.length] = Math.round(Math.random() * WinWidth) + WinWidth;
                        AsteriodXSpeed[AsteriodXSpeed.length] = Math.random() * 10 + 5;
                        AsteriodYSpeed[AsteriodYSpeed.length] = Math.random() * 7 - 5;
                        asteriods += 1;
                    }
                }
                ast[i].style.pixelLeft = AsteriodXpos[i];

                ast[i].style.pixelTop = AsteriodYpos[i]+hscrll;

               
            }




            if (ShipXpos - ShipXSpeed < -150) {
                ShipXpos = WinWidth;
            }
            else {
                if (ShipXpos - ShipXSpeed > WinWidth) {
                    ShipXpos = -150;
                }
            }

            if (ShipYpos - ShipYSpeed < -150) {
                ShipYpos = WinHeight;
            }
            else {
                if (ShipYpos - ShipYSpeed > WinHeight) {
                    ShipYpos = -150;
                }
            }
            ShipXpos -= ShipXSpeed;
            ShipYpos -= ShipYSpeed;
            shipLayer.style.pixelLeft = ShipXpos;

            shipLayer.style.pixelTop = ShipYpos + hscrll;


            DidCollide()
            sleep(10);
            setTimeout('fly()', 10);


        }



        //  End -->

        function displayunicode(e) {
            var unicode = e.keyCode ? e.keyCode : e.charCode;
            var keyPressed;
            keyPressed = (String.fromCharCode(unicode));
            moveShip(keyPressed)
        }

        function moveShip(keyPressed) {
            switch (keyPressed) {
                case "w" || "W":
                    if (ShipYSpeed != 25) {
                        ShipYSpeed += 5;
                    }
                    break;
                case "a" || "A":
                    if (ShipXSpeed != 25) {
                        ShipXSpeed += 5;
                    }
                    break; ww
                case "s" || "S":
                    if (ShipYSpeed != -25) {
                        ShipYSpeed += -5;
                    }
                    break;
                case "d" || "D":
                    if (ShipXSpeed != -25) {
                        ShipXSpeed += -5;
                    }
                    break;
            }
        }
        function DidCollide() {
            for (i = 0; i < asteriods; i++) {

                xDistance = AsteriodXpos[i] + 25 - ShipXpos - 75;
                yDistance = AsteriodYpos[i] + 25 - ShipYpos - 75;
                distance = xDistance * xDistance + yDistance * yDistance;
                if (distance < 4225) {

                    wasCollision = 1;
                    document.write('<img src="Explosion.png">');
                    var endTime = new Date();
                    var totalTime = new Date();
                    totalTime.setTime(endTime.getTime() - startTime.getTime());
                    test = "this is a test";
                    document.write('You managed to last ' + totalTime.getSeconds() + '.' + totalTime.getMilliseconds() + ' seconds.  You avoided a total of ' + AstriodsAvoided + 'astroids');

                }
            }
        }
        function sleep(milliseconds) {
            var start = new Date().getTime();
            for (var i = 0; i < 1e7; i++) {
                if ((new Date().getTime() - start) > milliseconds) {
                    break;
                }
            }
        }
    </script>
    <!-- Script Size:  3.79 KB -->
</body>
</html>
Any help someone could provide would be wonderful, and if you can explain to me whats going wrong and why its not working that would be even better!
Mcwatson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 05:04 PM.


Advertisement
Log in to turn off these ads.