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

2018/4/6

How do I initialise an actor?

Navique Navique

2018/4/6

#
I'm working on a Bullet Hell style game, and I have added a "Mana" mechanic, in which the user is able to use stronger attacks, at the cost of their mana being reduced. Mana regenerates over time, and I attempted to implement a Mana meter, where an Actor Rocket checks if it's mana is a certain number, and if it is, spawns an actor ManaSpec(number). This all works fine, until i try to remove the ManaSpec, where it says that the actor has not been initialised. The code usd is the one below. Keep in mind, the Mana variable and this code is exclusive to the Rocket actor, and the ManaSpec has no code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if  (mana >= 1000)
       {
           getWorld().addObject(new ManaSpec1(this), 583, 12);
            
       }
//code for spawning a manaSpec
if  (mana < 1000)
       {
           Actor ManaSpec1;
            
           World world;
           world = getWorld();
           world.removeObject(ManaSpec1);
            
       }
//code for removing
danpost danpost

2018/4/6

#
You are dealing with two different things here -- not one in the same. Yes, you have created and added a ManaSpec1 object into the world (regardless of where). However, the rocket has not retained a reference to it and you cannot just pull one out of nowhere (as your line 9 apparently was meant to do). Move line 9 to outside the method block it is in, changing ManaSpec1 to manaSpec1, and split up line 3 as follows:
1
2
manaSpec1 = new ManaSpec1(this);
getWorld()addObject(manaSpec1, 583, 12);
If you already have the variable declared outside the method, just make the change. Also, make the same change in line 13.
Navique Navique

2018/4/7

#
danpost wrote...
You are dealing with two different things here -- not one in the same. Yes, you have created and added a ManaSpec1 object into the world (regardless of where). However, the rocket has not retained a reference to it and you cannot just pull one out of nowhere (as your line 9 apparently was meant to do). Move line 9 to outside the method block it is in, changing ManaSpec1 to manaSpec1, and split up line 3 as follows:
1
2
manaSpec1 = new ManaSpec1(this);
getWorld()addObject(manaSpec1, 583, 12);
If you already have the variable declared outside the method, just make the change. Also, make the same change in line 13.
Sorry for replying again, but I don't seem to understand how to do the fix. I've updated the code, but I get a different error - can't find var manaSpec1, even though I seem to have it defined. Here is the updated code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if  (mana >= 1000)
       {
           manaSpec1 = new ManaSpec1(this);
           getWorld().addObject(manaSpec1, 583, 12);
            
       }
if  (mana < 1000)
       {
            
            
           World world;
           world = getWorld();
           world.removeObject(manaSpec1);
            
       }
danpost danpost

2018/4/7

#
Navique wrote...
I've updated the code, but I get a different error - can't find var manaSpec1, even though I seem to have it defined. Here is the updated code. << Code Omitted >>
You will need to post the entire class code for review.
Navique Navique

2018/4/7

#
danpost wrote...
Navique wrote...
I've updated the code, but I get a different error - can't find var manaSpec1, even though I seem to have it defined. Here is the updated code. << Code Omitted >>
You will need to post the entire class code for review.
Here it is: Be warned, it is long.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.*;
import greenfoot.*;
 
/**
 * Player controls
 *
 * @Author: Navique
 * @Version: 1
 */
public class Rocket extends Actor
{
        int ypos = 400
        int xpos = 300;
        int mana = 5000; //Base Mana level
        int ManaUpgrade = 0; //increased in MainMenu - Shop
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        int ypos = getY(); 
        int xpos = getX();
        mana = mana + 5;
        shoot();
        move();
        spell();
    }
    public void shoot ()
    {
        if (Greenfoot.isKeyDown("space")) //creates an new shot at rockets position
        
            getWorld().addObject(new NormalShot(this), getX(), getY());
        }
    }
    public void move ()//basic movement code
    {
     if (Greenfoot.isKeyDown("A"))
       {
         xpos = xpos - 4;
         setLocation(xpos, ypos); 
       }
     if (Greenfoot.isKeyDown("D"))
     {
         xpos = xpos + 4;
         setLocation(xpos, ypos);
     }  
     if (Greenfoot.isKeyDown("w"))
     {      
         ypos = ypos - 4;
         setLocation(xpos, ypos);
     }
     if (Greenfoot.isKeyDown("s"))
     {
         ypos = ypos + 4;
         setLocation(xpos, ypos);
     }  
         
    }
    public void spell () //unfinished extra abilites for Calamitas - mana regens over time
    {
        if (Greenfoot.isKeyDown("Q") & mana >= 1000)// Checks if Piercingshot can be spawned
        {
            getWorld().addObject(new ManaPierceShot(this), getX(), getY());//creates pierce shot
            mana = mana - 1000;
             
            //takes away mana
        }
        //Dash Spell code
        if (Greenfoot.isKeyDown("E") & Greenfoot.isKeyDown("W") & mana >= 500)
        {
            ypos = ypos - 30;
            mana = mana - 500;
            setLocation(ypos, xpos);
        }
        if (Greenfoot.isKeyDown("E") & Greenfoot.isKeyDown("S") & mana >= 500)
        {
            ypos = ypos + 30;
            mana = mana - 500;
            setLocation(ypos, xpos);
        }
        if (Greenfoot.isKeyDown("E") & Greenfoot.isKeyDown("A") & mana >= 500)
        {
            xpos = xpos - 30;
            mana = mana - 500;
            setLocation(ypos, xpos);
        }
        if (Greenfoot.isKeyDown("E") & Greenfoot.isKeyDown("D") & mana >= 500)
        {
            ypos = ypos + 20;
            mana = mana - 500;
            setLocation(ypos, xpos);
        }
         
        if (mana > (5 + ManaUpgrade) * 1000)
        {
            mana = (5 + ManaUpgrade) * 1000; //resets mana variable based on amount of upgrades bought
        }
    }
    public void ManaCheck ()//unfinished check for Manameter
    {
        Actor ManaSpec1;
        
       if  (mana >= 1000)
       {
           //manaSpec1 = new ManaSpec1(this);
           //getWorld().addObject(manaSpec1, 583, 12);
            
            
       }
       if  (mana >= 2000)
       {
           getWorld().addObject(new ManaSpec2(this), 547, 12);
            
       }
       if  (mana >= 3000)
       {
           getWorld().addObject(new ManaSpec3(this), 511, 12);
            
       }
       if  (mana >= 4000)
       {
           getWorld().addObject(new ManaSpec4(this), 475, 12);
            
       }
       if (mana >= 5000)
       {
           getWorld().addObject(new ManaSpec5(this), 438, 15);
       }
       if  (mana < 1000)
       {
            
            
           World world;
           world = getWorld();
          // world.removeObject(manaSpec1);
            
       }
       if  (mana >= 2000)
       {
           getWorld().addObject(new ManaSpec2(this), 547, 12);
            
       }
       if  (mana >= 3000)
       {
           getWorld().addObject(new ManaSpec3(this), 511, 12);
            
       }
       if  (mana >= 4000)
       {
           getWorld().addObject(new ManaSpec4(this), 475, 12);
            
       }
       if (mana >= 5000)
       {
           getWorld().addObject(new ManaSpec5(this), 438, 15);
       }
    }
}
danpost danpost

2018/4/7

#
Please also supply the code to the ManaSpec1 class so a better idea of what you are trying to do can be ascertained. You could also add a comment or two about what the purpose of the class is. Another question I have is this: do all your ManaSpec# classes use the same image? -- and if so, do they link together when side-by-side or are they each acting separately from each other?
Navique Navique

2018/4/8

#
danpost wrote...
Please also supply the code to the ManaSpec1 class so a better idea of what you are trying to do can be ascertained. You could also add a comment or two about what the purpose of the class is. Another question I have is this: do all your ManaSpec# classes use the same image? -- and if so, do they link together when side-by-side or are they each acting separately from each other?
The ManaSpecs have no code whatsoever, aside from allowing them to be used by the Rocket class. The rocket is what the player controls. All the ManaSpecs have the same image, but they do not link up.
danpost danpost

2018/4/8

#
Navique wrote...
The ManaSpecs have no code whatsoever, aside from allowing them to be used by the Rocket class. The rocket is what the player controls. All the ManaSpecs have the same image, but they do not link up.
Seems to me that they are an image set used to display a value -- in this case mana/1000. Maybe you should take a look at my Value Display Tutorial scenario.
You need to login to post a reply.