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

2015/1/21

Help Needed! creating an exception to getOneIntersectingObject

TheGoldenBucket TheGoldenBucket

2015/1/21

#
Hey guys! Need a bit of help with my game I am creating. Trying to create a "blade" object that flies out of your character when you push a button. Here is my current code:
public void rangedAttack()
    {
        if (Greenfoot.isKeyDown("o"))
        {
            Ranged attack = new Ranged();
            getWorld().addObject(attack, getX(), getY());
        }
    }
The problem is that this code below is preventing the "blade" from moving.
if (Greenfoot.isKeyDown("s"))
        {
            int x = getX();
            int y = getY();
            int ny = getY()+2;
            setLocation(x,ny) ;
            if (getOneIntersectingObject(null) != null)
            {
                setLocation(getX(), getY()-2);
            }
            charImage = new GreenfootImage("IMG_1190.PNG");
        }
This code is in every direction, including "w", "a", "s", and "d". Please help!
danpost danpost

2015/1/21

#
I do not believe that either code snippet has much to do with the movement of the "blade" object. There are a couple of issues from what I can see, however. One is that because you are using 'isKeyDown' unconditionally (without any more restrictions on creating blade objects) blade objects will be in mass production (one created every act cycle for as long as the trigger key, "o", is pressed down). Another is that in the second code snippet (well, I guess all movement key controls), your actor (character) will freeze until the "blade" object proceeds beyond its realm (until the images of the blade and the character no longer intersect). This is due to using 'getOneIntersectingObject(null)' which does not restrict which type of object it intersects. For the movement of your blade object, you need to post the code of your Ranged class.
TheGoldenBucket TheGoldenBucket

2015/1/23

#
Here it is!
   public void act() 
    {
       setLocation(getX(), getY()-10);
       setLocation(getX(), getY()-10);
       setLocation(getX(), getY()-10);
       getWorld().removeObject(this);
    }
Still working to see if I can throw delays into this so that it is not deleted immediately.
danpost danpost

2015/1/23

#
TheGoldenBucket wrote...
Still working to see if I can throw delays into this so that it is not deleted immediately.
Throwing delays into the code will cause all other actors to 'freeze' during the life of a blade. The act method is called repeatedly (usually about 50 to 60 times a second), so you do not need to repeat code within it (see lines 3 through 5 above). Also, a condition is required for removing the object. For a limited range object, giving it a 'life-span' might be best. You do this by counting act cycles executed (or moves taken):
private int age; // declare/initialize (default value is zero) age field
// act method
public void act()
{
    setLocation(getX(), getY()-10); // '10' is the speed of the blade
    age++; // count act cycles in world
    if (age == 20) // '20' is lifespan (adjust value as needed)
    {
        getWorld().removeObject(this); // remove from world
    }
}
The range of the blade is determined by the speed times its lifespan. Changing either of these values will alter the range of the blade.
TheGoldenBucket TheGoldenBucket

2015/1/23

#
Thanks, danpost! -TheGoldenBucket
TheGoldenBucket TheGoldenBucket

2015/1/24

#
When I pasted the code into my "Ranged" class, it gave me the error of "Illegal start of expression". Any way to fix that? It was giving the error at the beginning of the code.
danpost danpost

2015/1/24

#
You must not have inserted the code into your class properly. Please post the code for the class.
TheGoldenBucket TheGoldenBucket

2015/1/30

#
Never mind. Managed to get it fixed, and now I should be able to use this to create a directional system. Thanks! -TheGoldenBucket
You need to login to post a reply.