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

2013/1/15

help!

popiroprios popiroprios

2013/1/15

#
I need a code in which my player when it hits an object called a block, the actor can not get through
danpost danpost

2013/1/15

#
Will need to see what code you have for moving the actor. You should probably show the code for the class.
popiroprios popiroprios

2013/1/17

#
This is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class mario extends Actor
{
   int vf=0,vi=0,b=0, i=0, c=0,x=0,cont=0,count=0,conti=0;
   boolean derecha = false ;
   boolean izquierda = false;
   boolean vas = true;
   boolean detect = false;
   boolean dir = false;
   public double  salto()
    {
        
        vf=vi-10*2;
        return vf;
        
    }   
    
    public void act() 
    {
    if(Greenfoot.isKeyDown("right"))
    {
            setLocation(getX()+4, getY());
            
            c=1;
            detect = false; 
            dir = true;
    }
    if(Greenfoot.isKeyDown("left"))
    {
         setLocation(getX()-4, getY());
            
            b=1;
            detect = true ;
           dir = false;
    }
   
        if(c==1)
        {
          i++;
            setImage("m"+i+".png");
    }
        if(i==2)
    {
        i=0;
            c=0;
            setImage("m0.png");
    }
    if(b==1)
    {
        x++;
        setImage("a"+x+".png");
        
    }
    if(x==2)
    {
        x=0;
       b=0;
      setImage("a0.png");
         }
    if (Greenfoot.isKeyDown("up"))
    {
        derecha = true;
        izquierda = true;
    }
    if (derecha==true && dir== true) 
    {
        vi++;
        salto();
        setLocation(getX()+3,getY()+vf);
        setImage("j1.png");
    }
    if(detect==true && dir==false && izquierda==true)
    {
        vi++;
        salto();
        setLocation(getX()-3,getY()+vf);
        setImage("j2.png");
    
    
    
    
    }
    if(getY()>=355)
    
    {
        count++;
       if(derecha==true)
       {
         setImage("m0.png");
        }
         if( detect==true)
       {
         setImage("a0.png");
        }
        detect=false;
        derecha=false;
     izquierda=false;
        vi=0;
     vas= false;
    if(Greenfoot.isKeyDown("right"))
    { 
    setImage("m"+i+".png");
}
     if(Greenfoot.isKeyDown("left"))
     {
         setImage("a"+x+".png");
        }
    }
    if(vas==true)
{
if (Greenfoot.isKeyDown("up"))
    {
        derecha = true;
        izquierda = true;
    }
    if (derecha==true)
    {
        vi++;
        salto();
        setLocation(getX()+3,getY()+vf);
    }
 }
 if(vas==false)
{
cont++;
if(cont==3)
{
vas=true;
}
}
if(Greenfoot.isKeyDown("space"))
{        
        if (derecha==true)
        {
            conti=0;
            int a=getRotation();
            getWorld().addObject(new bala(a),getX(),getY());
        }
        if (derecha==false)
        {
             conti=0;
            int a=getRotation();
            getWorld().addObject(new bala2(a),getX(),getY());
        }
    }
if(getOneIntersectingObject(chompi.class)!=null)
{
getWorld().addObject(new lose(),269,221);
Greenfoot.stop();


}

}
}
    
      



danpost danpost

2013/1/17

#
I am getting lost in your code. You are using more fields than are neccessary; you have multiple places where the actor gets re-located; your variable naming is cryptic; and you have nothing really working in your code (animation does not seem to work; I could not say what half the 'if's are checking or what their block do; you have some code that could be simplified and some variables that are useless -- count and conti). You may want to start fresh. Maybe create another sub-class of Actor called 'Mario' (with a capital M) and built it up; each time you add something, make sure it works before you continue. Try to keep things simple. Choose field types that can give you the most information that you need. For instance, instead of using a boolean to track the horizontal direction, use an int. It not only can tell you the direction (by the sign of the value), but can also tell you how fast in that direction (by the value). The sign of the value can also be used to determine the image set to use (if not jumping). The same counter can be used for all non-jumping image sets, as opposed to multiple counters (x and i). The value of most of the booleans can be determined by data the is immediately accessable. Before moving the actor, determine where it is to go by using the horizontal and vertical speeds plus whatever is added due to key presses. Once you know where it is to go, move it there, check for intersectors, and if any, move it back. If not moving it back, change the image. There is a lot to think about, but doing one thing at a time, compiling and running the scenario each step along the way, and choosing meaningful field (or variable) names, will help in the long run.
You need to login to post a reply.