Author Topic: Bouncing PowerUps (now with collision issues!)  (Read 5116 times)

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Hello!

I'm currently looking at maybe something else to do this, but I'm hoping not to use it. So, here's what's going on.

I have a bullet group that kills an enemy group when the enemies health is 0. When this occurs, I want the enemy to drop a power-up type object that drops so the player can pick it up later. I'm having trouble with the randomization a bit. I have it set up to add a number every second. And in the function, if that number is equal to more or less a value then create a power up. Trouble is I'm only getting one power up to drop no matter the value I assign. Here's some code to look at:

Code: [Select]
if (FlxG.elapsed)
{
Drop_Rate += Math.random()* 7;
}
//before that, I had left out the math.random and just did this:
//Drop_Rate += FlxG.elapsed;
//Then once it got to 7, I had it reset like so:
//if (_Drop_Rate>= 7)
//            {
//                _Drop_Rate= 0;
//                            
//            }
//That was still giving the same results. Now for the enemy kill function, I do //this:
public function quake(Bullet:FlxSprite, DarkLord:FlxSprite):void
{
DarkLord.health -= 1;
Bullet.kill();
FlxG.play(soundFX2, 0.2, false);



if (DarkLord.health <= 0)
{
DarkLord.kill();
}

//Drops 1
if (DarkLord.health <= 0 && Drop_Rate ==3)
{


_hearts = new Hearts(DarkLord.x,DarkLord.y-12);
_hearts.velocity.y = 100;
add(_hearts);
}
if (DarkLord.health <= 0 && Drop_Rate ==6)
{


_hearts2 = new Hearts2(DarkLord.x,DarkLord.y);
_hearts2.velocity.y = 100;
add(_hearts2);
}
}
//here, If I were to use >= or <= instead of ==, I get a power up to drop, //but only one Kind of power up. Not one or the other.


Any ideas?
« Last Edit: Tue, Oct 12, 2010 by xhunterko »
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: PowerUp!
« Reply #1 on: Thu, Oct 7, 2010 »
I can't debug the code quickly, but I can offer a tip that I use.

Use FlxG.log() to send variables to console. Press the tilda key (`) to see it.

So maybe try:
FlxG.log("Drop rate is : " + Drop_Rate);

Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: PowerUp!
« Reply #2 on: Thu, Oct 7, 2010 »
Well, I do believe that helped fixed it. I don't know what was wrong with my other methods though. I'll post again once I'm happy with any results. Thanks!
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

SysOp

  • Member
  • **
  • Posts: 53
  • Karma: +0/-0
    • View Profile
    • check my kick ass half-life mod!
Re: PowerUp!
« Reply #3 on: Sat, Oct 9, 2010 »
Hey, here's what I use to spawn a random powerup on screen.

Code: [Select]
_powerup_time_stop = new Pickup(_player, 1);
_powerup_mine = new Pickup(_player, 2);

_powerups_group.add(_powerup_mine);
_powerups_group.add(_powerup_time_stop);

Update()

_timer_powerup += FlxG.elapsed;
if (_timer_powerup >= _powerup_spawn_time)
{
//make a random powerup appear
_powerups_group.members[Utils.randLong(0, _powerups_group.members.length-1)].appear(Utils.randLong(0, Utils.PLAYABLE_AREA_W), Utils.randLong(0, Utils.PLAYABLE_AREA_H));

_timer_powerup = 0.0;
_powerup_spawn_time= Utils.randLong(30, 180);
}

I use another function to spawn powerups when an enemy dies. Not random, but in your powerup class, you should have a function that randomizes the type. When you touch the powerup, you call the Kill function of the power up, and there, you do your magic.
"Hope is not a dream, but a way of making dreams become reality"

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: PowerUp!
« Reply #4 on: Sat, Oct 9, 2010 »
My tutorial http://www.omegastormproductions.com/uber-flash/flixel-2-tutorial-extended goes through adding random powerups to the Flixel 2 tutorial on http://www.creativeapplications.net/flash/flixel-2-tutorial-flash-tutorials-games/

It should give you a general idea of how to go about doing it.

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: PowerUp!
« Reply #5 on: Sun, Oct 10, 2010 »
Hey guys! Thanks!
(warning, wall of text)

I think it was your tutorial that helped me figure things out a bit omega. A problem I had with it was the random was not assigning 'whole' random numbers (ie 1.24444444 etc instead of just 1). So I did something based on the number of kills and decided to use that instead. I do however, have yet another bug.

I want the player to be able to catch or pick it up within a few seconds after it hits the ground. So there for, I want it to bounce for a few times and then fade out. I have tried several times now to get that to work. And no matter what collision/overlap/override I call, the velocity isn't just isn't changing. I don't know what's up. I thought that maybe my playstate was getting clogged and I had reached the maximum number of allowed overlaps. But I thought that silly, but still made a testing screen instead. And the same no-results happened. So, here's my simplified test state:

Code: [Select]
package 
{
import org.flixel.*;
//future sound reference: FlxG.play(SndClass,1,true);



public class TestScreen extends FlxState
{

[Embed(source="noise/softWind.mp3")] private var soundFX1:Class;
[Embed(source="pics/QUASI___.ttf", fontFamily = "quasi")] public var fontQuasi:String;

public var _hearts:Hearts;
public var _hearts2:Hearts2;
public var _heartz:FlxGroup;

public var _hill:Hills;
public var _tree:Tree;

override public function create():void
{



FlxState.bgColor = 0xffa51cFF;

//the tell the mouse to be shown with the icon here
//FlxG.mouse.show(ImgCursor);



_hill = new Hills(0, 177);
_hill.fixed = true;
add(_hill);

_tree = new Tree(80, 64);
_tree.fixed = true;
add(_tree);





FlxG.play(soundFX1, 1, true);

//press to start



//this is where we add the tilemap

}

override public function update():void
{
if(FlxG.keys.X)
{
this.onFade();
}

if(FlxG.keys.SPACE)
{
this.onFade3();
}
if(FlxG.keys.C)
{
_hearts = new Hearts(mouseX,mouseY-12);
add(_hearts);

_hearts2 = new Hearts2(mouseX,mouseY);
add(_hearts2);
}


super.update();

FlxU.collide(_heartz, _hill);
}

private function onFade():void
{
FlxG.fade.start(0xff69190C, 1.2, onFade2);



}

public function bounce(_hearts:Hearts, Hill:Hills):void
{

_hearts.velocity.y = -500;

}

private function onFade2():void
{

FlxG.state = new StoryScreen();

}

private function onFade3():void
{

FlxG.fade.start(0xff69190C, 1.2, onFade4);

}
private function onFade4():void
{

FlxG.state = new playstate();
}



}

}

My two "power up" classes
Code: [Select]
package
{
import org.flixel.*;
import org.flashdevelop.utils.FlashConnect;


public class Hearts extends FlxSprite
{
[Embed(source = "pics/lifeUp.png")] private var ImgHeart:Class;


private var Speed:Number;



public function Hearts(X:int, Y:int)
{
super(X, Y);
loadGraphic(ImgHeart, true, false, 16, 16);
this.acceleration.y = 100;

}


}
}
//they are virtually the same except for the the image, class, and embed name

The object I want the power ups to bounce off of
Code: [Select]
package
{
import org.flixel.*;
import org.flashdevelop.utils.FlashConnect;

public class Hills extends FlxSprite
{
[Embed(source = "pics/kkTerrain.png")] private var ImgHills:Class;


public function Hills(X:int, Y:int)
{
super(X, Y);
loadGraphic(ImgHills, true, false, 320, 64);

this.solid = true;
this.fixed = true;

width = 320;
height = 72;


}

override public function update():void
{

super.update();

}



}
}

Any ideas as to what I'm doing wrong again?
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: PowerUps (now bounce, dang-it!)
« Reply #6 on: Sun, Oct 10, 2010 »
Quote
Code: [Select]

           
 FlxU.collide(_heartz, _hill);
       
 public function bounce(_hearts:Hearts, Hill:Hills):void
   {
       
      _hearts.velocity.y = -500;
             
   }

Your bounce function isn't getting called. To do it like this, you'll have to use overlap. If you want to use collide, you'll want to override hitBottom in the powerup classes and change the velocity there.

As for making them disappear after a few seconds, I'd just create an internal timer on the powerup classes that will kill them once expired.

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: PowerUps (now bounce, dang-it!)
« Reply #7 on: Sun, Oct 10, 2010 »
Quote
Your bounce function isn't getting called. To do it like this, you'll have to use overlap. If you want to use collide, you'll want to override hitBottom in the powerup classes and change the velocity there.

I've done that too. Specifically:

Code: [Select]

FlxU.overlap(_heartz, _hill,bounce);

etc etc etc

public function bounce():void
{

_heartz.acceleration.y = -500;
//or
// _heartz.velocity.y = -500;
}

//where _heartz is a group
//and just recently

public function bounce(Hearts:FlxSprite, Hills:FlxSprite):void
{

_hearts.velocity.y = -500;

}

//as well as doing the above suggested of overriding the hit bottom and then //using collide as so

FlxU.collide(_heartz, _hill);

//in the _hearts,_hearts2 class
override public function hitBottom(Contact:FlxObject, Velocity:Number):void
{
velocity.y = -velocity.y/1.5;

}



Yeah, I got nothing.
*shrugs*
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: PowerUps (now bounce, dang-it!)
« Reply #8 on: Sun, Oct 10, 2010 »
Did you add the two heart powerups to the heartz FlxGroup?

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: PowerUps (now bounce, dang-it!)
« Reply #9 on: Sun, Oct 10, 2010 »
You mean this?:

Code: [Select]
_heartz.add(_hearts);
            _heartz.add(_hearts2);
            add(_heartz);


Ohhhh look, I have a blank color screen! And I can only add the hearts objects. That's just in my test state, play state doesn't do that. The other two objects dissappear. Another interesting note, is whenever I try to trace the velocity.y of either of the heart objects, the game freezes.
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: PowerUps (now bounce, dang-it!)
« Reply #10 on: Sun, Oct 10, 2010 »
Quote
Code: [Select]
if(FlxG.keys.C)
   {
     _hearts = new Hearts(mouseX,mouseY-12);           
     add(_hearts);   
                   
     _hearts2 = new Hearts2(mouseX,mouseY);
     add(_hearts2);       
   }

Try adding the hearts to the FlxGroup right here and see if it works.

e.g. _heartz.add(_hearts);

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: PowerUps (now bounce, dang-it!)
« Reply #11 on: Mon, Oct 11, 2010 »
I think, note, think, I have my problem solved. I think this:

Code: [Select]
this.acceleration.y = 100;
Was the problem. I'm gonna do some further tests before I'm absolutely sure. But thanks again guys!
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Bouncing PowerUps (now with collision issues!)
« Reply #12 on: Tue, Oct 12, 2010 »
So I have sort of the desired effect going on here:

http://www.swfcabin.com/open/1286906601

If you watch the hearts cloesely, you'll notice that they always don't 'bounce'. Sometimes they get stuck. And other times they just fall through. I had this issue in another game too. Any idea what's causing these hiccups or how to fix?

Here is source for the playstate:
(rather long but I'm pressed for time, i'll try and trunctuate it later)
Code: [Select]
package 
{
    import org.flixel.*;
    //future sound reference: FlxG.play(SndClass,1,true);
   
     
   
    public class TestScreen extends FlxState
    {
       
        [Embed(source="noise/softWind.mp3")] private var soundFX1:Class;
        [Embed(source="pics/QUASI___.ttf", fontFamily = "quasi")] public var fontQuasi:String;
        [Embed(source="noise/NoteA.mp3")] private var soundFX2:Class;
        [Embed(source="noise/NoteD.mp3")] private var soundFX3:Class;
       
        //counters for determining block fall rate
        public var _fallTimer: Number;
        public var _typeTimer: Number;
        //counters for determing block type rate
        public var _fallInterval: Number = 1.5;
        public var _typeInterval: Number = 0;
       
        public var _hearts:Hearts;
        public var _hearts2:Hearts2;
        public var _heartz:FlxGroup;
       
        public var _hill:Hills;
        public var _tree:Tree;
           
        override public function create():void
        {
           
            add(_hearts);
            add(_hearts2);
           
             FlxState.bgColor = 0xffa51cFF;

            //the tell the mouse to be shown with the icon here
            //FlxG.mouse.show(ImgCursor);
           
           
           
            _hill = new Hills(0, 177);
            _hill.fixed = true;
            add(_hill);
           
            _tree = new Tree(80, 64);
            _tree.fixed = true;
            add(_tree);
           
       
           
           
           
            FlxG.play(soundFX1, 1, true);
           
            resetFallTimer();
            resetTypeTimer();
           
           
           
            super.create();
        }
       
        override public function update():void
        {
            if(FlxG.keys.X)
                {
                    this.onFade();
                }
               
            if(FlxG.keys.SPACE)
                {
                    this.onFade3();
                }       
            if(FlxG.keys.C)
                {
                    _hearts = new Hearts(mouseX,mouseY-12);           
                    add(_hearts);   
                   
                   
                    _hearts2 = new Hearts2(mouseX,mouseY);
                    add(_hearts2);   
                   
                }   
           
           
            //Timers
                if(FlxG.elapsed)
                {
                    _typeInterval += 1;                   
                }
                if(_typeInterval >= 3)
                {
                    _typeInterval = 0;                   
                }
                //block timers
                _fallTimer -= FlxG.elapsed;
               
                if (_fallTimer < 0)
                {
                    spawnBlock();
                    resetFallTimer();
                }
               
                if (_typeTimer < 0)
                {
                    spawnBlock();
                    resetTypeTimer();
                }
                //End block timers
           
           
            //FlxU.overlap(_hearts2, _hill, bounce2);
               
            super.update();
            //FlxU.collide(_hearts, _hill);
           
           
            FlxU.overlap(_hearts, _hill, bounce);     
            FlxU.overlap(_hearts2, _hill, bounce2);
        }
         
       
        private function resetFallTimer():void
        {
       
            _fallTimer = _fallInterval;
            _fallTimer *= 0.65;//95
            if (_fallInterval < 0.1)
            {
            _fallInterval = 0.3;    //1
            }
               
        }
       
        private function resetTypeTimer():void
        {
       
            _typeTimer = _typeInterval;
            _typeTimer *= 8;
            if (_typeInterval < 4)
            {
            _typeInterval = 0;   
            }
               
        }
        private function onFade():void
        {
            FlxG.fade.start(0xff69190C, 1.2, onFade2);
           
           
       
        }
       
        private function spawnBlock():void
        {
         
            var x: Number = Math.random() * (FlxG.height/2) + 50;
            var y: Number = 0;
            //each block is designated two numbers of the interval that way the spread is more even
            if (_typeInterval==1||_typeInterval==2)
            {
            _hearts = new Hearts(x+20*Math.random(), y);
            add(_hearts);
           
            }
            if (_typeInterval==2||_typeInterval==3)
            {
            _hearts2 = new Hearts2(x+20*Math.random(), y);
            add(_hearts2);
           
            }
           
           
           
        }
       
        public function bounce(Heart:Hearts,Hill:Hills):void
        {
            if( Heart.health > 0)
            {
            Heart.velocity.y *= -0.888;
            Heart.health += 1;
            }
           
            FlxG.play(soundFX2, 1, false);
           
        }
        public function bounce2(Heart2:Hearts2,Hill:Hills):void
        {
            //RedB.velocity.y *=-0.888;
            if( Heart2.health > 0)
            {
            Heart2.velocity.y *= -0.888;
            Heart2.health += 1;
            }
            //Heart2.kill();
           
            FlxG.play(soundFX3, 1, false);
           
        }
        private function onFade2():void
        {
         
            FlxG.state = new StoryScreen();
           
        }
       
        private function onFade3():void
        {
         
            FlxG.fade.start(0xff69190C, 1.2, onFade4);
       
        }
        private function onFade4():void
        {
           
            FlxG.state = new playstate();
        }
       
       
       
    }
   
}

Both the updated hearts:
(the issues still occur whether or not the override hitBottom is in there or not, so I don't know if that's the issue)
Code: [Select]
package
{
    import org.flixel.*;
    //import org.flashdevelop.utils.FlashConnect;
   

    public class Hearts extends FlxSprite
    {
        [Embed(source = "pics/lifeUp.png")] private var ImgHeart:Class;
       
       
        private var Speed:Number;
   
       
       
        public function Hearts(X:int, Y:int)
        {
            super(X, Y);
            loadGraphic(ImgHeart, true, false, 16, 16);
            velocity.y = 180;
            health = 3;
        }
       
        override public function hitBottom(Contact:FlxObject,Velocity:Number):void
            {
            //FlxG.play(bopHit, 1, false);
           
           
            velocity.y *= -velocity.y/0.888;
           
            }
       
    }
}
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: Bouncing PowerUps (now with collision issues!)
« Reply #13 on: Tue, Oct 12, 2010 »
Just a guess here, but it could be a problem with using overlap.

Heart.velocity.y *= -0.888;

It's possible that the bounce function is getting called repeated and making the velocity change over and over between a positive and negative number. I'd test my theory by changing that to just a static negative number and seeing what it does.

e.g. Heart.velocity.y = -100;

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Bouncing PowerUps (now with collision issues!)
« Reply #14 on: Tue, Oct 12, 2010 »
That causes them do go up just a wee bit too high. It's almost as if there's two conflicting numbers somewhere that cause them to behave wierd. I just don't know where they are. At least, that's what I'm seeing.
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

OmegaStorm

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • OmegaStorm Productions
Re: Bouncing PowerUps (now with collision issues!)
« Reply #15 on: Tue, Oct 12, 2010 »
Well, it was mostly meant as a test to see if none of them got stuck or fell through.

I wonder if calling a super.update() in the heart classes would help.

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Bouncing PowerUps (now with collision issues!)
« Reply #16 on: Tue, Oct 12, 2010 »
None that I can see. An earlier problem I discovered was during the spawn sequence. If a heart spawned at the same time as another heart was hitting the hill object, the heart calling the collision, wouldn't collide and it would just fall through faster instead. I'm wondering if that's the problem. And if it is, I really don't want to make 4/8 more extra objects just to get it to look right.
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Bouncing PowerUps (now with collision issues!)
« Reply #17 on: Thu, Oct 14, 2010 »
Just to update that I got this fixed. I had to call collision for each individual sprite against a group. Well, I think I can move on now.
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha