This site requires JavaScript, please enable it in your browser!
Greenfoot back
Christopher
Christopher wrote ...

2013/10/24

Smashing head into keyboard | please help |

Christopher Christopher

2013/10/24

#
Creating a shooting game i run into an what seemed simple at first but unsolvable problem for me. I want (on keypress) lightning to shoot up out of the world // working Lightning to find enemy and setLocation on that enemy // working Want lightning to stay at that exact location while running rest of code // not working, keeps following enemy as enemy moves. following code is the working part to setLocation of that enemy. (and i dont want it to move cause it needs to strike, needs to be there, expand and dissapear.
public void seaker2()
{          
   if (counter > 100) 
   {
        int enm =0;         
        int range= getWorld().getWidth()*getWorld().getHeight();
        List<Enemy> l = (List<Enemy>) getObjectsInRange(range,Enemy.class);
        if (l.size() >= 1)
         {                          
             int e = l.get(enm).getX();
             int f = l.get(enm).getY()-(getImage().getHeight()/2);               
             setImage(l6); 
                         
             setLocation(e,f); 
         }             
   } 
                      
        else if (l.size() <= 0) { getWorld().removeObject(this); }
}
Hope some1 understands my question and can help me. Like i said. All i want is for it to just stay on location its given. Tried lotta different options for almost 2 days now... cant figure it iout. Please advise. Thanks
erdelf erdelf

2013/10/24

#
u should save the coords of the enemy in the first run, and then use them instead of the actual coords
Christopher Christopher

2013/10/24

#
And i really want that! yes! that would be perfect... but im not sure how to save the coord. Any toughts on that? maybe i've just been staring at my screen for to long but im drawing a blank....
erdelf erdelf

2013/10/24

#
ok, the seaker2 method seems to be called every act cycle, so, write a new boolean called first and two new integer variables, lets call them x and y
    
private boolean first;
private int x,y;
then set first to false in the constructor and then rewrite ur method to this.
public void seaker2()  
    {            
       if (counter > 100)   
       {  
         if(!first)
         {
            int enm =0;           
            int range= getWorld().getWidth()*getWorld().getHeight();  
            List<Enemy> l = (List<Enemy>) getObjectsInRange(range,Enemy.class);  
            if (l.size() >= 1)  
             {                            
                    x = l.get(enm).getX();  
                    y  = l.get(enm).getY()-(getImage().getHeight()/2);
             } else if (l.size() <= 0) { getWorld().removeObject(this); return; }  
             first=true;
         }
             setImage(l6);   
                               
             setLocation(x,y);   
                            
     }   
 }  
not tested
Christopher Christopher

2013/10/24

#
Tested.... and working! Thank you sOOooo much Erdelf! I can finally sleep tonight! You are my new hero! :) will analyse further on the exact workings but im happy it does what i want it to now.. ty ty ty ty!
You need to login to post a reply.