Forum Topic: set coordinates outside stge random

(73 views • 11 replies)

This topic is 1 page long.

<< < > >>
None

iluvAS

Reply To Post Reply & Quote

Posted at: 9/26/09 01:44 PM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

im making a defender game and i want it in a way that when i create the enemy its coordinates is set at random OUTSIDE the stage not within the players sight. How do i go about doing this?


None

Xeptic

Reply To Post Reply & Quote

Posted at: 9/26/09 01:47 PM

Xeptic LIGHT LEVEL 30

Sign-Up: 05/12/05

Posts: 2,182

Use negative values? Or values larger than the stage's boundaries?

BBS Signature

None

iluvAS

Reply To Post Reply & Quote

Posted at: 9/26/09 01:50 PM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

erm kay larger values liek if my stage width is 1000 and height 600

enemy[i].x=-50+Math.random()*1100
enemy[i].y=-50+Math.random()*700

//50 pixel tolerance each side

this will get the chance of it appearing ofscreen but theres also chance it appears within the stage.... so like... how do i spawn it out of screen??!


None

Xeptic

Reply To Post Reply & Quote

Posted at: 9/26/09 01:56 PM

Xeptic LIGHT LEVEL 30

Sign-Up: 05/12/05

Posts: 2,182

Ah like so... Hmm, you could randomize the values: So when (for example) Math.random is smaller than 0.5 you pick a negative x value, when it is higher you pick a positive x value. And you do the same for the y. But I'm sure there are smarter ways to do this.

BBS Signature

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/26/09 01:56 PM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,585

enemy[i].x=-50+Math.random()*1100
enemy[i].y=-50+Math.random()*700

//50 pixel tolerance each side

really? think about it. Lets only work on the x axis and you will soon see your mistake.
Math.random forms a number between 0 and 1. there for Math.random*1100 means it lowest number is 0, and highest 1100, and unless your stage width is a negative value, it will apear on screen. how about this (assuming your stage is 1000 pix wide)

(Math.random*1000)+1000

will spawn a x position between 1000 (on your stage tip) and 2000 (far from stage tip)

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

iluvAS

Reply To Post Reply & Quote

Posted at: 9/27/09 03:03 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

well okay i get ur point there but using that code i will only get xcoordinates on the right of the screen..
and im trying to make the enemies come from all corners and sides so im really confused on doing that. the only possible solution i think is... (Width 1000, Height 600)

do{
enemy[i].x=(Math.random()*1100)-50
enemy[i].y=(Math.random()*700)-50
while((enemy[i].x>0&&enemy[i].x<1000)||(enemy[i].y>0&&enemy[i].y<600));

//but i think this might cause some lagg issues with alot of mobs =(

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/27/09 03:16 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,585

if(Math.random>.5){
_x = -_width
} else {
_x = 1000+_width
}
if(Math.random>.5){
_y = -_height
} else {
_y = 800+height
}

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

iluvAS

Reply To Post Reply & Quote

Posted at: 9/27/09 03:31 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

o_O im not quite sure how that works... the width is the stages width or the mobs width? im using AS3 btw... i tried ur code but refrenced the width and height as the mobs width and height and i kept on getting it spawned off screen but on the corners and never appeared on the edges.... thankz for the help though


None

UnknownFury

Reply To Post Reply & Quote

Posted at: 9/27/09 03:37 AM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,027

There's probably a better way than this, but this is certainly better than your do.. while loop:

Math.random()*-50 + (stage.stageWidth+50)*Math.round(Math.random())

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/27/09 03:40 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,585

At 9/27/09 03:31 AM, iluvAS wrote: im using AS3 btw...

umm, try this?

if(Math.random>.5){
MC.x = -MC.width
} else {
MC.x = 1000+MC.width
}
if(Math.random>.5){
MC.y = -MC.height
} else {
MC.y = 800+MC.height
}

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

iluvAS

Reply To Post Reply & Quote

Posted at: 9/27/09 04:05 AM

iluvAS NEUTRAL LEVEL 07

Sign-Up: 11/18/07

Posts: 271

thankz aot for the help but i think me method was most effective

enemy.x=(Math.random()*1100)-50
enemy.y=(Math.random()*700)-50
while(enemy.x>-15&&enemy.x<1015&&enemy.y>-15&&enemy.y<615){
	enemy.x=(Math.random()*1100)-50
	enemy.y=(Math.random()*700)-50
}//set Position

i checked out another defender tutorial and it did somewhat the same thing.
http://www.internaldrive.com/2009/02/25/
video-tutorial-creating-a-defender-flash -game/

thankz though


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/27/09 04:08 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,585

whatever works for u

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 07:25 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!