sprite1.x = 4.8 and 14.6 etc., How to check for sprite1 location on screen ?

29 views
Skip to first unread message

vmars316

unread,
Apr 24, 2013, 8:14:45 PM4/24/13
to enchantjs-...@googlegroups.com
Hello & Thanks ,
I am learning both javascript and enchant.js at the same time, pls excuse my simpleton questions, Thanks.
On a  game screen of 320, 320 
I need to know when sprite is almost at rightMost location . 
So in my code i check for :
if (sprite1.x < 284 & sprite1.x > 280)
but i never get a hit .
So i displayed sprite1.x and i discovered it has values like: 
 sprite1.x = 0, 4.8, 9.6, 14.4, etc., (so far , increments of 4.8 )

So how can I find out sprite1 location on the screen ?   

Thanks..vm



vmars316

unread,
Apr 25, 2013, 2:47:20 PM4/25/13
to enchantjs-...@googlegroups.com
Come on  guys , give me a hand...
The whole code is below : 
The problem area is in the statement 
                if (Obama1.x <284 & Obama1.x> 280) 
                  {               
as the sprite moves across the screen it should pass thru atleast position 288.

 
<-! LiveSampleObamaTest.html ->
    enchant (); / / initialize
            var newLieCheck = 9;
            var oldLieCheck = 0; / /  
            var alertCount = 0;      
window.onload = function () {    
    var game = new Game (320, 320); / / game stage
    game.fps = 20;

    game.onload = function () {
        var Obama2 = new Sprite (64, 64);
              Obama2.image = game.assets ['http://www.vmars316.com/ShowMe/sprites2.png'];
              game.rootScene.addChild (Obama2);
              Obama2.frame = [0]; / / select sprite frame    
              Obama2.opacity = 0.0; 
        var Obama1 = new Sprite (64, 64); / / after Obama2 ensures Obama1 is on top initially 
              Obama1.image = game.assets ['http://www.vmars316.com/ShowMe/sprites1.png'];
              game.rootScene.addChild (Obama1);
              Obama1.frame = [0]; / / select sprite frame

        var Label4X = new Label ('Touch me!');
        Label4X.x = 100;
        Label4X.y = 80;
        game.rootScene.addChild (Label4X);
        game.rootScene.backgroundColor = '# FFF8DC';
                  
              Obama1.onenterframe = function () { 
                if (alertCount <4)
                  {
                alert ("Obama1.onenterframe"); 
                alertCount = alertCount +1;  
                Label4X.text = ("Obama1.x =" + Obama1.x);  
                  }
                if (Obama1.x <284 & Obama1.x> 280) 
                  {               
                   if (alertCount <4)
                     {
                     alert ("if (Obama1.x <288 & Obama1.x> 286)"); 
                     alertCount = alertCount +1;  
                     }
                  }   
/ * {
                     switch (oldLieCheck)
                       {
                         case 0 / / oldLieCheck
                             Obama1.opacity = 1.0;   
                             Obama2.opacity = 0.0; 
                         break; 
                       }
                     newLieCheck = 0; / / do math random here 
                     switch (newLieCheck)
                       {
                         case 0
                             Obama1.opacity = 0.0; 
                             Obama2.opacity = 1.0; 
                         break;
                       }
                 } / / If (Obama1.x <288 and   
* /                 
              }; / / Obama1.onenterframe = function ()  
        
        Obama1.tl.moveBy (288, 0, 60) / / move right
            . ScaleTo (-1, 1, 10) / / turn left  
            . MoveBy (-288, 0, 90) / / move left
            . ScaleTo (1, 1, 10) / / turn right
            . Loop (); / / loop it

        Obama2.tl.moveBy (288, 0, 90) / / move right
            . ScaleTo (-1, 1, 10) / / turn left  
            . MoveBy (-288, 0, 90) / / move left
            . ScaleTo (1, 1, 10) / / turn right
            . Loop (); / / loop it
    };

    game.start (); / / start your game!
    };

vmars316

unread,
Apr 27, 2013, 4:57:55 PM4/27/13
to enchantjs-...@googlegroups.com
I added :
an .addEventListener(Event.ENTER_FRAME
I thing are working fine ...vm
Reply all
Reply to author
Forward
0 new messages