Author Topic: Bullet rules  (Read 2530 times)

Fabrice

  • Visitor
  • *
  • Posts: 3
    • View Profile
    • Email
Bullet rules
« on: 2012-04-18, 01:10:52 AM »
Hi Everyone, I begin to learn SGDK2, it's the greatest application for making game I've tried. The scrolling are fast and very smooth.  :)

I want make a simple game to begin like a shooter game with an horizontal scrolling (something like R-type). I've a starship who must fire some bullets. What is the rules for making that ? When I press button all the bullets starts but I don't know when I must desactivate them. I've a message that indicate the limite of number of sprites is exceed.
Is there a rule to know if a sprite is out of the screen ?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bullet rules
« Reply #1 on: 2012-04-18, 05:09:59 AM »
1. Right-click on "SourceCode" folder and select "New"
2. Enter the name SpriteCustom.cs
3. Replace the whole text of the file with this:
Code: [Select]
using System.ComponentModel;
using System.Diagnostics;

public abstract partial class SpriteBase : GeneralRules
{

   /// <summary>
   /// Determine if this sprite is currently visible
   /// </summary>
   /// <returns>True if the sprite is active and any part of the
   /// sprite insersects with the currently displayed area of the
   /// map layer it is on, otherwise false.</returns>
   [Description("Determine if this sprite is currently visible")]
   public bool IsVisible()
   {
      return isActive && layer.IsSpriteVisible(this);
   }
}
Now when you go to edit Sprite rules, you will see a new rule "IsVisible".

If you are creating the game to also work in a browser (if you want to use the "Export to HTML5 Code" command in the file menu), then you must also do this:
1. Right-click on "SourceCode" and select "New"
2. Enter the name SpriteCustom.js (notice this name ends with ".js" instead of ".cs")
3. Add this to the file:
Code: [Select]
Sprite.prototype.isVisible = function() {
   return this.isActive && this.layer.isSpriteVisible(this);
};
4. Right-click on "SourceCode" and select "New"
5. Enter the name MapLayerCustom.js
6. Add this to the file:
Code: [Select]
MapLayer.prototype.isSpriteVisible = function(sprite) {
   if (!sprite.isActive)
      return -1;
   var x1 = Math.floor(sprite.x);
   var w1 = sprite.getSolidWidth();
   var x2 = -this.currentX;
   var w2 = viewWidth;
   var y1 = Math.floor(sprite.y);
   var h1 = sprite.getSolidHeight();
   var y2 = -this.currentY;
   var h2 = viewHeight;

   if ((x1 + w1 > x2) && (x2 + w2 > x1) && (y1 + h1 > y2) && (y2 + h2 > y1))
      return true;
   return false;
}

Now your rules will work in a web browser game too.

Fabrice

  • Visitor
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Bullet rules
« Reply #2 on: 2012-04-18, 05:26:00 AM »
Thanks a lot! Ben  :)

I tried your code and it's works fine. I thought all the sprites parameters were available in the GUI of SGDK2. I'm not an expert for coding scripts.

Thanks again. 

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bullet rules
« Reply #3 on: 2012-04-18, 06:31:28 AM »
I guess IsVisible was never implemented for a sprite. But fortunately SGDK2 is flexible enough that it could be added. In case anybody wants an SGDK2 file from which to import a bullet sprite and related code (or to see a running sample) I have attached one here.

Fabrice

  • Visitor
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Bullet rules
« Reply #4 on: 2012-04-18, 09:26:51 AM »
Great !!!

Thanks for the demo.