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

2017/1/23

cutting background images

Alexlazea Alexlazea

2017/1/23

#
Does anyone knows a program for cutting the background of images ? and i mean cutting it not just making it transparent. As an example if I want to make some spike for a game where whenever an actor touches the spikes they die, the actors die if they just touch the background. P.S: I'm not an english native, so sorry for my grammar
danpost danpost

2017/1/23

#
You can have each spike have two inner class objects that traverse the diagonal side edges of the spike as hit-boxes for collision detection. For example, if I can do it right without teting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*;
 
public class Spike extends Actor
{   
    protected void addedToWorld(World world)
    {
        new Side(true);
        new Side(false);
    }
 
    public class Side extend Actor
    {
        private Side(boolean isLefty)
        {
            Actor owner = Spike.this; // the spike that created this side
            GreenfootImage ownerImg = owner.getImage();
            int imgWide = ownerImg.getWidth();
            int imgHigh = ownerImg.getHeight();
            int length = (int)Math.hypot(imgWide/2, imgHigh);
            setImage(new GreenfootImage(length, 2));
            int angle = (int)(Math.atan2(imgHigh, imgWide/2)*180/Math.PI);
            int x = owner.getX();
            int offsetX = imgWide/4;
            int sign = isLefty ? -1 : 1;
            setRotation(angle*sign);
            owner.getWorld().addObject(this, x+offsetX*sign, owner.getY());
        }
         
        public void act()
        {
            if (Spike.this.getWorld() == null) getWorld().removeObject(this);
        }
    }
}
Then your player can do something like this:
1
if (isTouching(Spike.Side.class))
Alexlazea Alexlazea

2017/1/23

#
could you expain me what you did there ? i'm a begginer in greenfoot, i started to study greenfoot a few days ago honestly i understand almost nothing from what you answerd me :D I understand the idea, but not the code
danpost danpost

2017/1/23

#
When a Spike object is added into a world, it creates two different Spike.Side objects (one for the left side and one for the right, distinguished by the boolean value passed to it). The Spike.Side objects then get the image of the spike that created it and determines how long and what angle they need to be, creates their images and adds themselves to the world that their spike is in, at the appropriate places (that is all in the constructor of the Spike.Side class). The act method of the Side class has the Side objects continuously checks to see if their spike is still in the world; if not, they will remove themselves, also. If you have specific question about a line of code, specify.
danpost danpost

2017/1/23

#
I added some documentation here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import greenfoot.*;
 
/** class that creates Spike typed actors */
public class Spike extends Actor
{   
    /** things to do when the Spike actor is added into a world */
    protected void addedToWorld(World world)
    {
        new Side(true); // create left side hit actor
        new Side(false); // create right side hit actor
    }
     
    /** class to create objects that act as hitbox actors for a Spike actor */
    public class Side extend Actor
    {
        /** constructor of Side actors of a Spike actor */
        private Side(boolean isLefty)
        {
            Actor owner = Spike.this; // the spike that created this side
            GreenfootImage ownerImg = owner.getImage(); // gets spike image
            // get dimensions of spike image
            int imgWide = ownerImg.getWidth();
            int imgHigh = ownerImg.getHeight();
            // calculate length from a bottom corner to middle of top side of spike image
            int length = (int)Math.hypot(imgWide/2, imgHigh);
            // create and set appropriate image for this Side actor
            setImage(new GreenfootImage(length, 2));
            // determine number of degrees to turn the side
            int angle = (int)(Math.atan2(imgHigh, imgWide/2)*180/Math.PI);
            // get x-coordinate of Spike actor and determine horizontal offset to place side
            int x = owner.getX();
            int offsetX = imgWide/4;
            // convert boolean 'isLefty' value to a numeric in the set { -1, 1 }
            // value will be used for turning and offsetting in opposite directions
            int sign = isLefty ? -1 : 1;
            // set angle and add self into world at appropriate position
            setRotation(angle*sign);
            owner.getWorld().addObject(this, x+offsetX*sign, owner.getY());
        }
         
        // side removes itself from world when it spike is removed
        public void act()
        {
            if (Spike.this.getWorld() == null) getWorld().removeObject(this);
        }
    }
}
Hope this helps.
Alexlazea Alexlazea

2017/1/23

#
I belive i still have a lot to learn, i understood your idea and i'm gonna apply it differently, made by myself, othervise i will not understant anything and not learn anything Thans for the ideea dude, you're awesome
You need to login to post a reply.