Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums [AS2] Number/Damage help
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 04-06-2012, 04:39 PM #1 MAGG0T Member   Join Date: Mar 2012 Posts: 64 [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!
 04-06-2012, 06:16 PM #2 MAGG0T Member   Join Date: Mar 2012 Posts: 64 SOLVED _root.content.window.hp.health -= 4 + Math.ceil(Math.random()*6);
 04-06-2012, 06:16 PM #3 Noct Toys in the Attic...     Join Date: Oct 2006 Location: NY Posts: 3,022 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))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
 04-06-2012, 06:38 PM #4 MAGG0T Member   Join Date: Mar 2012 Posts: 64 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(); }```
04-11-2012, 05:21 PM   #5
Noct
Toys in the Attic...

Join Date: Oct 2006
Location: NY
Posts: 3,022

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
 healthBar.zip (7.2 KB, 19 views)
__________________
http://rwithers.com

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 07:41 PM.

///