Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-21-2012, 12:07 PM   #1
Pkiller162
Member
 
Join Date: Apr 2012
Location: UK
Posts: 78
Default Do not allow placing of movieclip

I have a movieclip called "Block" which is 20x20px big and have a function which creates these blocks:

Code:
public function createblock():void
		{
			var fx:Number = Math.floor(player.x / 20) * 20
			var fy:Number = Math.floor(player.y / 20) * 20
			block = new Block(blockselected)
			block.x = fx
			block.y = fy
			addChild(block)
			blockarray.push(block)
			trace(blockarray.length)
			swapChildren(block, player)
		}
I use the fy and fx codes to line the blocks up nicly with each other
My problem is that you can place blocks on top of each other.
Is their a code that states: If block at position fx,fx do not place block
or a code like: If space fx,fy = empty, place block

Thanks

Last edited by Pkiller162; 05-21-2012 at 12:10 PM.
Pkiller162 is offline   Reply With Quote
Old 05-21-2012, 12:43 PM   #2
Scearezt
Satisfied User
 
Scearezt's Avatar
 
Join Date: Jun 2010
Location: C:\\System32\Hungary
Posts: 426
Lightbulb

Well, you should create a manager for them, to keep tracking them. Like when you create a block, push it into the manager's array, and give it an index.
Scearezt is offline   Reply With Quote
Old 05-21-2012, 12:59 PM   #3
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

Quote:
Originally Posted by Pkiller162 View Post
Is their a code that states: If block at position fx,fx do not place block
or a code like: If space fx,fy = empty, place block

Thanks
yes there is, that.

you have to make the structure for it to do anything though.

try making a 2D array, and put each block into the appropriate [x][y] element in the array. you'll probably end up with huge gaps in your arrays but that's fine, as3 supports sparse arrays.

then you can just check if a specific array element contains anything, before putting something in it. and it's array position can also be its world position.
WarKirby is offline   Reply With Quote
Old 05-21-2012, 02:32 PM   #4
Pkiller162
Member
 
Join Date: Apr 2012
Location: UK
Posts: 78
Default

Ok, I think I did something wrong

Code:
var blockmanager:Array = new Array()

Code:
public function createblock():void
		{
			var fx:Number = Math.floor(player.x / 20) * 20
			var fy:Number = Math.floor(player.y / 20) * 20
			if (blockmanager[fx, fy] == undefined)
			{
				block = new Block(blockselected)
				block.x = fx
				block.y = fy
				addChild(block)
				blockarray.push(block)
				blockmanager[fx,fy] = block
				trace(blockarray.length)
				setChildIndex(block, numChildren - 1)
				setChildIndex(player, numChildren - 1)
				for (var i:int = 0; i < dotarray.length; i++)
				{
					setChildIndex(dotarray[i], numChildren - 1)
				}
				setChildIndex(hud, numChildren - 1)
				setChildIndex(selecter,numChildren-1)
				for (i = 0; i < 4; i++)
				{
					setChildIndex(getChildByName("hudblock"+i), numChildren - 1)
				}
			}
		}
EDIT: Ignore the display list code near the bottom, I just just testing some things

EDIT 2: Damn, no 2d arrays in as3! >

EDIT 3: Ok, need some help now D:

Last edited by Pkiller162; 05-21-2012 at 03:11 PM.
Pkiller162 is offline   Reply With Quote
Old 05-21-2012, 03:45 PM   #5
Sonny
Senior Member
 
Join Date: Aug 2010
Location: Sydney,Australia
Posts: 132
Default

Quote:
If block at position fx,fx do not place block
From what I understand your fx,fy depends on the position of your player .And hence it might not be a linear array. So you should probably be looking at collision of any point in a rectangle (fx,fy),(fx+20 , fy +20)

So what I would probably do is create your object the way you have created. Once added to the stage do a hitTestObject on all the previous elements in your list array and if it comes out false push it into your list array.Else either remove or move your object and add it to the manager array when you find it is in a correct position. If it is just a point you are going to check you can use a hitTestPoint to decide if the point might overlap any of your other objects probably.

A manager class might be useful to help you narrow down the number of objects you are doing a hittest with and blind collision detection can slow down your code execution , especially because number of detections increases exponentially with each object being added.

Hope that helps

Last edited by Sonny; 05-21-2012 at 03:48 PM.
Sonny is offline   Reply With Quote
Old 05-21-2012, 03:55 PM   #6
Pkiller162
Member
 
Join Date: Apr 2012
Location: UK
Posts: 78
Default

I also thought of this, thanks :P
Pkiller162 is offline   Reply With Quote
Old 05-21-2012, 04:34 PM   #7
Pkiller162
Member
 
Join Date: Apr 2012
Location: UK
Posts: 78
Default

Quote:
Originally Posted by Sonny View Post
From what I understand your fx,fy depends on the position of your player .And hence it might not be a linear array. So you should probably be looking at collision of any point in a rectangle (fx,fy),(fx+20 , fy +20)

So what I would probably do is create your object the way you have created. Once added to the stage do a hitTestObject on all the previous elements in your list array and if it comes out false push it into your list array.Else either remove or move your object and add it to the manager array when you find it is in a correct position. If it is just a point you are going to check you can use a hitTestPoint to decide if the point might overlap any of your other objects probably.

A manager class might be useful to help you narrow down the number of objects you are doing a hittest with and blind collision detection can slow down your code execution , especially because number of detections increases exponentially with each object being added.

Hope that helps
Grrr. This is annoying me now. Your method works, yet I cannot place blocks next to each other!

snpr.cm/cnM3bn.png

The above picture shows a simple example of what I mean. The space inbetween each block is big enough to fit another block (20x20) yet that counts as a hit on block.hitTestObject

Any ideas?
Pkiller162 is offline   Reply With Quote
Old 05-21-2012, 11:21 PM   #8
Sonny
Senior Member
 
Join Date: Aug 2010
Location: Sydney,Australia
Posts: 132
Default

Sorry had to head off to bed yesterday. Did you manage to figure out your solution ??? If not can you post a little bit more info/ code regarding that. Your problem doesn't seem to be the actual collision detection .It is how you do it or how you handle the result.

Cheers
Sonny is offline   Reply With Quote
Old 05-21-2012, 11:28 PM   #9
Pkiller162
Member
 
Join Date: Apr 2012
Location: UK
Posts: 78
Default

Well I have to head off to bed now XD
I tried adding the x and y values to a 2D array, which im currently having some trouble with. The thread is called 2D array problem or something
Thanks for all your help
I will reply in the morning
Night
Pkiller162 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 04:18 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.