Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-06-2012, 04:39 PM   #1
MAGG0T
Member
 
Join Date: Mar 2012
Posts: 64
Default [AS2] Number/Damage help

Im trying to add a random damage is delt when the Enemy attacks in my game here is the code i have

Code:
onClipEvent (load)
{
    
    var scale = this._xscale;
}
onClipEvent (enterFrame)
{
    var distance = 150;
    var tx = this._x;
    var ty = this._y;
    var sx = _root.content.window.hero._x;
    var sy = _root.content.window.hero._y;
    if (Math.sqrt((sx - tx) * (sx - tx) + (sy - ty) * (sy - ty)) < distance)
    {
        if (tx < sx)
        {
            this.gotoAndStop(3);
            this._xscale = scale;
        } // end if
        if (tx > sx)
        {
            this.gotoAndStop(3);
            this._xscale = -scale;
        } // end if
    } // end if
    if (Math.sqrt((sx - tx) * (sx - tx) + (sy - ty) * (sy - ty)) > distance)
    {
        _root.content.window.enemy.gotoAndStop(1);
    } // end if
    if((_currentframe==3)and(this.hitTest(_root.content.window.hero))) {
_root.content.window.hp.health--


    } // end if
   
}

As you can see _root.content.window.hp.health-- is only dealing -1 per hit the enemy makes Lets say i want the damage to be random and between 5-10 how would i do this?
Thanks in advance!
MAGG0T is offline   Reply With Quote
Old 04-06-2012, 06:16 PM   #2
MAGG0T
Member
 
Join Date: Mar 2012
Posts: 64
Default

SOLVED _root.content.window.hp.health -= 4 + Math.ceil(Math.random()*6);
MAGG0T is offline   Reply With Quote
Old 04-06-2012, 06:16 PM   #3
Noct
Toys in the Attic...
 
Noct's Avatar
 
Join Date: Oct 2006
Location: NY
Posts: 3,017
Default

Try this:
ActionScript Code:
onClipEvent (load) {     var scale = this._xscale;     //Random method - input low and high values, returns ran between the two     function rN(lowValue:Number, highValue:Number):Number {         return ((Math.random()*(lowValue-highValue-1)) >> 0)+highValue;     } } onClipEvent (enterFrame) {     var distance = 150;     var tx = this._x;     var ty = this._y;     var sx = _root.content.window.hero._x;     var sy = _root.content.window.hero._y;     if (Math.sqrt((sx-tx)*(sx-tx)+(sy-ty)*(sy-ty))<distance) {         if (tx<sx) {             this.gotoAndStop(3);             this._xscale = scale;         }         if (tx>sx) {             this.gotoAndStop(3);             this._xscale = -scale;         }     }     // end if     if (Math.sqrt((sx-tx)*(sx-tx)+(sy-ty)*(sy-ty))>distance) {         _root.content.window.enemy.gotoAndStop(1);     }     if ((_currentframe == 3) and (this.hitTest(_root.content.window.hero))) {         //----------------------------------------------         //Call method, get random num:         var randomNumber:Number = rN(5, 10);         //Subtract var by that number (-=)         _root.content.window.hp.health -= randomNumber;         //----------------------------------------------     } }
__________________
http://rwithers.com
Noct is offline   Reply With Quote
Old 04-06-2012, 06:38 PM   #4
MAGG0T
Member
 
Join Date: Mar 2012
Posts: 64
Default

Nope that didn't work but i was able to solve the problem with this
_root.content.window.hp.health -= 4 + Math.ceil(Math.random()*6);

You be able to help me with this though....
iv made a health bar that works
but i want the health to = 200
and the health bar to remain 100 in width
but when i change the health to 200 the width of the health bar also doubles to 200


Code:
onClipEvent (load){
    var health = 100;
}
onClipEvent (enterFrame)
{
    setProperty("", _width, health);
    if (health<=0)
    {
       _root.content.gotoAndStop("failgame");


    } // end if
    updateAfterEvent();
}
MAGG0T is offline   Reply With Quote
Old 04-11-2012, 05:21 PM   #5
Noct
Toys in the Attic...
 
Noct's Avatar
 
Join Date: Oct 2006
Location: NY
Posts: 3,017
Default

Hah, it didn't work!? It's your code maggot... all I did was add in my randomNumber generator, that I have been using in extensively QA'd commercial software for almost a decade, so I can assure you, it works... =)

As far as your other question... Sounds like all you need to do is divide the health variable in half before you apply it to the bar... Something to the effect of:
ActionScript Code:
healthBarMc.fillMc._width = (healthNum/2);
Attached Files
File Type: zip healthBar.zip (7.2 KB, 18 views)
__________________
http://rwithers.com
Noct is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:57 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.