Skip to main content

News

Topic: Shooting and Collision Detection (Read 11391 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Re: Shooting and Collision Detection
Reply #15
Programmings just like that though, such small things are easily missed out and can be hard to realize.
;) Its a great reference for a shooting-bullet code though, thank you so much.

I'm having trouble implement it.

for the off-map representation of the bullet, I threw it in a Bullet.js script file

same with the code for when your hero creates a bullet but I think I need an if-statement and GetKey(); function for the trigger-button/input from the player. I think this might need to be in an update or game script file.

the code that is attached to the map engine I'm guessing could go in a seperate script for the general map engine

I think the collision detection would go in the same script file as when the player creates a bullet, maybe that can also go in Bullet.js

same with the entity check code

Still I have no idea what is the right way to use such a script in sphere, the bullet code is basically controlling a person on a map so I'm guessing I might as well all just throw it in a mapengine or update script or both but

Code: (javascript) [Select]

function Update()
{
  // your update script stuff
  UpdateBullets();
}

function game() {
    // your game code

    SetUpdateScript("Update()");
    MapEngine("map.rmp", 60);
}


by your update stuff you meant bullet update code stuff or just general update stuff?
same with game code?

I think If yes I would make a MapEngine.js and throw the entire code in there then have the function Update() to handle the rest of the code inside. I would call RequireScript(MapEngine.js) in the Startscript.js but how do I set it as a MapEngine script... I think if I can understand where and how to manage script files in Sphere I could be on my way to trying out this code and making my own for other stuff very smoothly. For now its an enigma.
  • Last Edit: July 07, 2013, 03:58:08 pm by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting and Collision Detection
Reply #16
I'll make a test demo, complete with all the resources and upload it here. Just wait it'll take a day to do.

Okay, I attached a demo project for you to learn from. :)
  • Last Edit: July 07, 2013, 11:17:25 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Shooting and Collision Detection
Reply #17
 8) Your the man!


Re: Shooting and Collision Detection
Reply #18
Noticed this

Code: (javascript) [Select]

var mw = GetLayerWidth(0) * GetTileWidth();
var mh = GetLayerHeight(0) * GetTileHeight();


Whats going on here?

Code: (javascript) [Select]

while (AreKeysLeft())
{
if (GetKey() == KEY_SPACE) CreateBullet("player");
}


What function can I use to check only if the player has pressed the key down. I want it so that even if the space-bar is held continuously only one bullet is fired.
  • Last Edit: July 08, 2013, 06:23:19 am by Xenso

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Shooting and Collision Detection
Reply #19

Noticed this

Code: (javascript) [Select]

var mw = GetLayerWidth(0) * GetTileWidth();
var mh = GetLayerHeight(0) * GetTileHeight();


Whats going on here?

I haven't checked the script, but that gets you the size of the map in pixels. GetLayerWidth() and GetLayerHeight() get the size of a layer in tiles (assuming all layers are the same size here), so it has to be multiplied with the tile size itself.

Re: Shooting and Collision Detection
Reply #20
That makes sense now.

I found the functoin  isKeyPressed(); in the API Functions list ( Sphere Wiki ), Is there an isKeyReleased function?
I was thinking of making a variable to assign itself to != isKeyPressed(); but I have a big feeling that's going overboard and will not work.
The variable will be too big and I think this would kill computer memory.

Is there a way to limit the shooting to one shot per button press? I replaced the while loop with this code but it made the shooting more like a flame-thrower than what I wanted. ( hmm I'll keep that in mind maybe for a power up  :P )

Code: (javascript) [Select]

//If the player presses space
if (IsKeyPressed(KEY_SPACE)){ CreateBullet("Player")}

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Shooting and Collision Detection
Reply #21

I was thinking of making a variable to assign itself to != isKeyPressed(); but I have a big feeling that's going overboard and will not work.
The variable will be too big and I think this would kill computer memory.


Wh... why would it be too big? A boolean variable takes up a couple bytes in memory, you have billions of bytes to work with... ???
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting and Collision Detection
Reply #22
Assigning a variable in a loop, just assigns the same data location over and over, it doesn't add more to memory. If computers were implemented that way we'd still be in the technological stone age! :P

To hit a key only once per click, the easiest way to do that is like this:
Code: [Select]

var key_hit = false;

function Update()
{
    if (IsKeyPressed(KEY_CTRL) && !key_hit)
    {
        do_something();
        key_hit = true;
    }
    if (!IsKeyPressed(KEY_CTRL)) key_hit = false;
}
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Shooting and Collision Detection
Reply #23
Code: (javascript) [Select]
var key_hit = false;

function Update()
{
UpdateBullets();
if (!IsKeyPressed(KEY_Z)) key_hit = false;

if (IsKeyPressed(KEY_Z) && key_hit == false)
{
key_hit = true;
CreateBullet("ghost");
}


The code works perfect now. Thanks to all your help.  ;D
  • Last Edit: July 09, 2013, 07:08:49 am by Xenso