This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:guides:beginners:shooting_and_spawners [2018/02/13 20:47 (7 years ago)] – ↷ Page moved from guides:beginners:shooting_and_spawners to en:guides:beginners:shooting_and_spawners iarwain | en:guides:beginners:shooting_and_spawners [2024/11/18 05:08 (4 months ago)] (current) – Fix position and speed of the bullets sausage | ||
---|---|---|---|
Line 3: | Line 3: | ||
Time for our hero to defend himself in the quest to reach the prize. He needs to shoot his gun. | Time for our hero to defend himself in the quest to reach the prize. He needs to shoot his gun. | ||
- | What will we use for a bullet? There a few choices, but we'll go for particle.png in the data/object folder: | + | What will we use for a bullet? There a few choices, but we'll go for '' |
{{ : | {{ : | ||
- | Define | + | Once copied, define |
< | < | ||
Line 20: | Line 20: | ||
</ | </ | ||
- | Any BulletObject that is created will be scaled down a little, sent flying off to to the right, and can only exist for 1 second before auto-destroying. | + | Any BulletObject that is created will be scaled down a little, sent flying off to to the right, and can only exist for 1 second before |
- | What is the best way to get our hero to shoot lots of these [BulletObject]s? A spawner is the thing to use. | + | What is the best way to get our hero to shoot lots of these '' |
- | A spawner can be attached to an object (like our hero) and it can be told to spawn a specific type of object (like a bullet). Let's take this approach to start with. It won't be perfect, but we can always improve it later. | + | A spawner can be attached to an object (like our hero). And it can be told to spawn a specific type of object (like a bullet). Let's take this approach to start with. It won't be perfect, but we can always improve it later. |
Create a spawner for the hero: | Create a spawner for the hero: | ||
Line 36: | Line 36: | ||
</ | </ | ||
- | Some words on the above. | + | This spawner can shoot '' |
- | Time to attach it to the HeroObject: | + | Time to attach it to the '' |
- | < | + | < |
[HeroObject] | [HeroObject] | ||
Graphic | Graphic | ||
- | Position | + | Position |
Scale = 2 | Scale = 2 | ||
AnimationSet = HeroAnimationSet | AnimationSet = HeroAnimationSet | ||
Line 54: | Line 54: | ||
{{: | {{: | ||
- | Great so that's working for us. Now how to stop it from shooting. The only command | + | Great, so that's working for us. Now, how to stop it from shooting. The function |
- | If we disabled our hero object, yes it would shop him from shooting, but it would also make him dissapear. Not quite what is expected. | + | If we disabled our hero object, yes it would stop him from shooting, but it would also make him disappear. Not quite what is expected. |
There are a number of ways to solve this situation. Let's go with a simple one. | There are a number of ways to solve this situation. Let's go with a simple one. | ||
Line 62: | Line 62: | ||
What if the HeroObject had a single empty child object? And what if that child object had the spawner? Because it would be invisible, it wouldn' | What if the HeroObject had a single empty child object? And what if that child object had the spawner? Because it would be invisible, it wouldn' | ||
- | Define | + | Define |
< | < | ||
[HerosGun] | [HerosGun] | ||
Spawner | Spawner | ||
- | Position | + | Position |
</ | </ | ||
- | Remove the spawner from the hero object. Make HerosGun a child of HeroObject: | + | Remove the spawner from the hero object, and make '' |
- | < | + | < |
[HeroObject] | [HeroObject] | ||
Graphic | Graphic | ||
- | Position | + | Position |
Scale = 2 | Scale = 2 | ||
AnimationSet = HeroAnimationSet | AnimationSet = HeroAnimationSet | ||
Line 82: | Line 82: | ||
</ | </ | ||
- | When you run this, you'll notice little difference. Our hero is still firing. But now we can turn his gun off at the bottom of the Init() function. | + | When you run this, you'll notice little difference. Our hero is still firing. But now we can turn his gun off at the bottom of the '' |
But we need a reference to the gun in code, which we don't have yet because [HerosGun] was created automatically with HeroObject. | But we need a reference to the gun in code, which we don't have yet because [HerosGun] was created automatically with HeroObject. | ||
Line 88: | Line 88: | ||
First declare the gun as a variable just like the hero object: | First declare the gun as a variable just like the hero object: | ||
- | < | + | < |
orxOBJECT *hero; | orxOBJECT *hero; | ||
orxOBJECT *herosGun; | orxOBJECT *herosGun; | ||
</ | </ | ||
- | Then, in the Init() function, get the child reference of the gun using the hero object: | + | Then, in the '' |
- | < | + | < |
- | herosGun = (orxOBJECT*)orxObject_GetChild(hero); | + | hero = orxObject_CreateFromConfig(" |
+ | | ||
+ | orxObject_CreateFromConfig(" | ||
</ | </ | ||
- | Now that you have the reference, turn off the object at the bottom of the Init() function: | + | Now that you have the reference, turn off the '' |
- | < | + | < |
- | orxObject_Enable(herosGun, | + | hero = orxObject_CreateFromConfig(" |
+ | herosGun = (orxOBJECT*)orxObject_GetChild(hero); | ||
+ | | ||
+ | orxObject_CreateFromConfig(" | ||
</ | </ | ||
Compile and run. | Compile and run. | ||
- | Cool, he stopped shooting. The last thing, turn on, and off the shooting based on pressing the left control key: | + | Cool, he stopped shooting. The last thing, turn on, and off the shooting based on pressing the left control key in the '' |
< | < | ||
Line 120: | Line 125: | ||
Compile and run. | Compile and run. | ||
- | Hmmm... | + | Hmmm... |
- | < | + | < |
[BulletSpawner] | [BulletSpawner] | ||
Object | Object | ||
WaveSize | WaveSize | ||
WaveDelay | WaveDelay | ||
- | Position | + | Position |
- | ObjectSpeed | + | ObjectSpeed |
UseRelativeSpeed = true | UseRelativeSpeed = true | ||
</ | </ | ||
- | The ObjectSpeed property overrides the Speed property in the BulletObject (no it's not really needed anymore) and the UseRelativeSpeed means the ObjectSpeed with be applied relative to the direction of the spawner. | + | The '' |
Compile and Run. | Compile and Run. | ||
- | Fantastic! Our hero can now run, jump and shoot. | + | Fantastic! Our hero can now run, jump, and shoot. |
---- | ---- | ||
- | Next: [[guides: | + | Next: [[en:guides: |
- | {{section> | + | {{section> |