locked
javascript will not call another function? RRS feed

  • Question

  • Hi people, I am developing a game in HTML5 for windows eight, I am having a ton of trouble with getting my player image to move, I had it moving, but it left a trail of images behind it, I made a function to delete it, but now I cant even move the player
    <script>
    var currentX = 200;
    var currentY = 245;
    
    // remove current image
    
    	 deleteOldImageR();
    	{
             ctx.drawImage(img, currentX, currentY);
             img.src=black.png;
             moveAlien(15);
             alert ("it works");
    	};
    	
    	 deleteOldImageL();
    		{
    	         ctx.drawImage(img, currentX, currentY);
    	         img.src=black.png;
    	         moveAlien(-15);
    	         alert ("it works");
    		};
    
    
    function init()
    {
    	document.onkeypress = checkForPress;
    
    	moveAlien(0);
    
    }
    
    function moveAlien(xCoordIncr)
    {
    	var canvas = document.getElementById('myCanvas');
    	var context = canvas.getContext('2d');
    
    	
    	currentX += xCoordIncr;
    
    	
    
    	var imageObj = new Image();
    
    	imageObj.onload = function() {
    	context.drawImage(imageObj, currentX, currentY);
    	};
    
    
    	
    	imageObj.src = 'runner.gif'
    }
    
    function checkForPress(evt)
    {
    if ( evt.keyCode == 97 )
    {
    //alert ("go left");
    deleteOldImageL();	
    //moveAlien(-15);
    	
    }
    else if ( evt.keyCode == 100 )
    {
    deleteOldImageR();
    //moveAlien(15);
    }
    }
    
    </script>
    I commented out some unnecessary procedures that I used to test it
    Sunday, December 23, 2012 11:07 PM

Answers

All replies

  • Hi,

    Because I can't reproduce your scenario on my side. So I suggest you debug that with vs 2012.

    #Quickstart: Debugging apps (JavaScript)

    http://msdn.microsoft.com/en-us/library/windows/apps/hh441474.aspx


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by RunMyServerINC Monday, December 24, 2012 1:12 PM
    Monday, December 24, 2012 6:39 AM
  • Syntactically your functions for deletion of images are incorrect.  You may not be receiving an error in terms of code hinting but these are not correct function declarations and they will never execute.

    you have plenty of function declarations in your code already that are correct - change these to use a correct format e.g "function deleteOldImage () {...}.

    FYI the way you have this coded, you have a function call followed by an object declaration.  That is, "deleteOldImage();" is a function call.  The bit after in braces is treated as an object.  This code inside the object will never execute and the function call will generate errors in your debugger if you run the code.

    Tuesday, December 25, 2012 9:24 PM