Shooting on touch

TF101

  • *
  • Posts: 49
Hello,
once again i need help from the community.
In one of my levels, the user can trigger a shot by touching the screen. That works so far.
But at certain angles the shot does not appear on the cannon but on the top right or top left, depending on where the player is on the screen.

I have attached 2 sample pictures to illustrate the problem. The last picture is the code I use for this.
Maybe someone can tell me what I'm doing wrong or how I can solve it better so that the shot is created exactly at the cannon.

Kind regards, Thomas

Luyren

  • *
  • Posts: 2574
Radius is the distance from the actor's center. Angle modifier is in case you want to deviate from your actor's direction. By your screenshots, you probably don't need an angle modifier. You can replace "direction self" with your angle attribute.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

TF101

  • *
  • Posts: 49
I guess i'm acting very stupid right now. Please forgive me!

I tried to recreate your example on myself. Unfortunately it does not work. No bullet will be created and the game will freeze.


Luyren

  • *
  • Posts: 2574
Radius is the distance from the actor's center. Angle modifier is in case you want to deviate from your actor's direction. By your screenshots, you probably don't need an angle modifier. You can replace "direction self" with your angle attribute.
If it wasn't obvious, you replace the words "Radius" and "angle modifier" with the appropriate values you need.

That said, the fault is on me for not being more specific, so no worries.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

TF101

  • *
  • Posts: 49
Hello,
Thanks for your answer. Now I am completely confused.
Unfortunately, I don't understand what you're trying to teach me.

I have made a example game . Maybe you or someone else can take a look inside. The problem is i can not use x-center/y-center of self when i create the missle, i must use x on screen because the scene is longer then the device display.
The game is for mobile devices, but the example should also work in flash.



Luyren

  • *
  • Posts: 2574
As I said, replace the words with the appropriate values for your game.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

TF101

  • *
  • Posts: 49
Thank you so much!!
It seems that solved the problem!
Now i must sit and try to understand the code.

Thanks again for your help!

Thomas

Luyren

  • *
  • Posts: 2574
It's trigonometry. Instead of "position of self plus offset" to determine your bullet's position, that code is based on a circle of a given radius (the distance between the center of your actor and the bullet), and using the cosine and sine functions of the angle you want to determine the position of your bullet along that circle. That's how you handle that for actors that can rotate.

So if you really want to understand it, look up trigonometry and the sine/cosine functions.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

TF101

  • *
  • Posts: 49
Oh god, that was more than 30 years ago.  But you are absolutely right.  It's just math.  And to learn and practice stencyl, this is an excellent example.