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

2015/3/3

attack the direction im looking thanks !

CodeDaySwag CodeDaySwag

2015/3/3

#
so im new to programming and i have 1 attak so far and it shooting but it keeps shooting to my rite how can i make it shoot in what ever direction im looking...once again im new so can you right out the code and explane...sorry for my english i am from middle east
danpost danpost

2015/3/3

#
You just need to set the rotation of the shot to that of the actor creating it. So, if you are shooting a 'Bullet' object, then in the class of the shooter:
1
2
3
Bullet bullet = new Bullet(); // create the object being shot
getWorld().addObject(bullet, getX(), getY()); // add the shot to the world
bullet.setRotation(getRotation()); // set rotation of the shot
CodeDaySwag CodeDaySwag

2015/3/3

#
well still shoots to the rite but i think it is bcz i have my own player grafics and when they move it does not change the rotation it changes the picture
danpost danpost

2015/3/3

#
Maybe you are setting the rotation within the class of the "Bullet" as well, ruining the setting when added into the world. Changing the graphics does not effect the rotation unless your new graphics is not facing right to begin with when at zero rotation.
CodeDaySwag CodeDaySwag

2015/3/3

#
im not setting any rotations within the code of the bullet or player
CodeDaySwag CodeDaySwag

2015/3/3

#
i want it whenever i move up it shoots up, down shoots down etc...
danpost danpost

2015/3/3

#
Does your player move left and right, then? Do you have a field to show which direction it is facing? I would help if you posted the code for the class of your player. Click on the link below the reply box 'Posting code? read this!' before posting the code.
CodeDaySwag CodeDaySwag

2015/3/3

#
yes my player moves left and right
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public void move()
    {
        int x = getX();
        int y = getY();
        //moving
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-2);
            setImage(WalkBack);
            if ( isTouching(Walls.class) )
            {
                setLocation(getX(), getY()+2);
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX(), getY()+10);
 
                 
            }
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+2);
            setImage(WalkDown);
            if ( isTouching(Walls.class) )
            {
                setLocation(getX(), getY()-2);
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX(), getY()-10);
 
                 
 
            }
 
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
            setImage(WalkLeft);
            if ( isTouching(Walls.class) )
            {
                setLocation(getX()+2, getY());
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX()+10, getY());
 
                
 
            }
 
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
            //setImage("NAME");
            if ( isTouching(Walls.class) )
            {
                setLocation(getX()-2, getY());
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX()-10, getY());
 
                
 
            }
 
        }
        //moving
 
    }
danpost danpost

2015/3/3

#
Alright, you can tell the direction facing by the image being used. So, change my last line above to this:
1
if (getImage() == WalkLeft) bullet.setRotation(180);
CodeDaySwag CodeDaySwag

2015/3/4

#
thanks it worked sorry took so long to anser
CodeDaySwag CodeDaySwag

2015/3/4

#
i still have a question tho how can i make messages like zelda and pokemon and make it disappear after pressing "whatever" key
danpost danpost

2015/3/5

#
First create a subclass of Actor to display the message; then give it the behavior (using the act method) to remove itself upon "whatever" key press.
You need to login to post a reply.