I'm using box2dflash for as3 and i need to create curvy ground like this.
Facts:
I can't create one solid object as far as I know because box2d support convex objects;
I didn't find any line-based objects.
Question:
Is there any better way than doing this the way shown on image below?
I couldn't say for sure, but I think that is the best way of doing it.
There is some code on this article for doing a similar thing:
Terrain like tiny wings
You will want to look at the "drawHill" function (Line 91). Sorry I can't be of much more help, I have limited experience with box2D.
I've discovered that you actually can create line.
var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
groundFixtureDef.density = 1;
var someShape: b2PolygonShape = new b2PolygonShape();
someShape.SetAsArray( new Array(new b2Vec2(-3,0),new b2Vec2(2,0)), 2);
groundFixtureDef.shape = someShape;
This would create a simple line. But if you add more points, it'll form convex shape.
Related
Im currently learning as3 but I come from several years of as2 experience, so what I was used to do is attach movieclips using string with a numeric variable to get the right identifier like this and maybe I was thinking about using eval here but of course its not available in as3 so here is what I was doing in as2:
currentlevel=1;
myString = "levelMc"+currentlevel;
And then of course use the constructed string in "attachMovie" function to get the right clip attached.
attachMovie(myString,myString+mydepth,mydepth);
But I see in as3 all is about classes and all different so I cant seem to find a way to dynamically get the right class name for the child to be added, is there an actual way to make that possible? because as far as I have gone I see that I should get the class name to make new object and then put that in "addChild" so therefore I cant find a way to pass any kind of dynamically constructed string or element refering my needed clip there, is it still possible?.
Thanks beforehand for any clues =)
EDIT I added the attachmovie call as a clarifying line, I know all you got what I was asking but as I cant find a right approach I prefer to clarify. Additionally I shouldnt have mentioned the "eval" here, sorry, I just can ask my as2 with string usage
EDIT Solved, thanks to you all guys, you saved me, I wish I could give you all kudos because all of your ideas and tips are awesome and really helpful, I marked that answer as correct because it was the one that lead me to getDefinitionByName which is what Im going to use in the end, but as said all tips and ideas to face new as3 world will help me greatly, so as said I wish I could give reputation to you all, thanks a lot =)
You still can set a name for your object, for instance:
var mc : MovieClip;
for (var i : int = 0; i < 5; i++) {
mc = new MovieClip();
mc.name = "myMc"+ i;
addChild(mc);
}
and you can get a specific child using:
this["myMc1"] as MovieClip
or
this["myMc"+ i] as MovieClip;
How about a simple array?
Because if you only need to access a set of levels in a ascending numeric fashion, this approach is easier.
var levels:Array = new Array();
levels[0] = //your level1 movieclip
levels[1] = //your level2 movieclip
...
function jumpToLevel(index:int):void{
//remove added levels beforehand
removeChildren(); //assuming the only children you added to this displaylist
//are levels, otherwise you will need another approach
addChild(levels[index]);
}
In AS2, we would store information about instances in instance name - button_0, button_1, etc. In AS3, you don't need/use instance names for anything other than keeping your code clear to you and to reference stuff you create by hand.
Use arrays to reference your instances. So, if you're making buttons, you create a button and push it into an array of buttons. Then just reference it as aMyButtons[i] instead of mcMyButtons["button_" + i]
Here's example code.
function fCreateButtons(): void {
var i: int = 0;
for (i = 0; i < 5; i++) {
var mcNewButton: McButton = new McButton();
mcNewButton.x = i * 200;
mcNewButton.iButtonNumber = i;
mcNewButton.tMain.text = "About Us";
mcNewButton.bButton.addEventListener(MouseEvent.MOUSE_DOWN, fButtonPress);
mcNewButton.bButton.addEventListener(MouseEvent.MOUSE_OVER, fButtonOver);
mcNewButton.bButton.addEventListener(MouseEvent.MOUSE_OUT, fButtonOut);
aButtons.push(mcNewButton);
}
}
function fButtonPress(e: MouseEvent): void {
var iButtonNumber: int = e.currentTarget.parent.iButtonNumber;
trace("fButtonPress(): iButtonNumber: " + iButtonNumber);
}
This is how you'd add the buttons to a holder clip and remove them.
mcButtonsHolder.addChild(aButtons[i]);
mcButtonsHolder.removeChild(aButtons[i]);
Well as I said above I managed to solve it finally using getDefinitionByName which is the closer I think I can get to "eval", that function managed to bring dinamycally formed class name to be used to call the new clips on run time which is perfect for me, at least make the code cleaner and easier to change if needed. As I said in last edit, thanks a lot to you all who helped, all your tips and ideas will be of great use for me in the as3 time, wish I could give you all reputation, thanks a lot =).
I have been making a basic painting application similar to MS-Paint with basic paint, eraser and fill tools. It's this last one that's giving me some trouble.
I'm pretty new to using BitmapData but the idea is that when the user clicks the board, it triggers the startFloodFill method. This is shown below:
public static function startFloodFill(e:MouseEvent):void
{
trace("FLOODFILL");
var boardRef:MovieClip = e.currentTarget.parent.board; //Creates a reference to the board
var boardData:BitmapData = new BitmapData(boardRef.width, boardRef.height); //Creates a new BitmapData with the same size as boardRef
boardData.floodFill(e.localX, e.localY, 0x00CCCCCC); //Applies the FloodFill
boardData.draw(boardRef); //Saves the boardRef as bitmapData
boardRef.bitmapData = boardData; //Updates the board
boardRef.parent.addChild(boardRef);
}
Can anybody tell me what I've done wrong here? When I click, the board does not change. I expected the FloodFill to fill the entire bitmap with the chosen colour as the board is blank when I click.
I also tried replacing the last two lines with:
boardRef.addChild(new Bitmap(boardData) ); //Updates the board
Thanks
The problem is that you first use the floodFill, and then use the draw method, which actually fills the BitmapData with whatever param you give it - in your case it's the boardRef.
You should first draw the boardRef into the boardData, and then use floodFill. At the end, you need to create new Bitmap and display it.
Now you are setting the bitmapData of a MovieClip?! Don't know what you wanted, but you just need to add new child (new Bitmap)
I think it is a simple question, but it really confuse me...
In the case which I try to show three object together, like Fibers, Volume and Mesh, just as you can see in this demo web.
Demo Web: http://goo.gl/NP2eUo
But there is a problem, the Fibers object can not automatic alignment with Volume and Mesh. Those files are come from the same source (one subject's DICOM file) so it should can be match together in default.
If only show the Fibers object, it can be put in the center of the view. But if show three object together, then Fibers object will be Shift!!!
Is there any possible way can let three object automatic alignment together?
And here is my source code: http://goo.gl/Ttzc84
What if you try to call reset boundingBox on showtime?
...
// .. add the mesh
r.add(mesh);
r.onShowtime = function() {
r.resetBoundingBox();
}
r.render();
...
How did you generate the surface and the trk file? Freesurfer and DTK?
I suspect the trk file does not provide any IJKToRAS transformation.
I have recently started developing with Libgdx. Now, I'm looking at collision detection for custom shapes. In my case, I want to detect the collision of a shark with other objects. As a shark is a custom shape, I used the Physics Body Editor (https://code.google.com/p/box2d-editor/downloads/detail?name=physics-body-editor-2.9.2.zip&can=2&q=) to transform the shape into a json format.
I already have the code for the image drawing of the sharks and other stuff, but now I have no clue of how to implement the detection of collission with the json file. The tutorial on the phycics body editor website uses a different approach than I do.
Right now, I am drawing my shark like this in my render method:
batcher.draw(sharkAnimation, shark.getX(),
shark.getY(), shark.getWidth(), shark.getHeight());
sharkAnimation is a TextureRegion, and shark is an object with an X, Y, width and height.
The sharks width and height are variable, but maintain the same ratio's.
I already got the bodyeditor for libgdx, and I'm experimenting with the following code, but honestly I have no clue of how I should handle this.
BodyEditorLoader loader = new BodyEditorLoader(
Gdx.files.internal("data/shark.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.position.set(0, 0);
bd.type = BodyType.DynamicBody;
// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;
loader.attachFixture(????, ????, ???, ????);
Help is greatly apreciated.
You need to create a body, and then use the loader the attach the fixture you created in the editor to that body.
Body body = getWorld().createBody(bd);
loader.attachFixture(body, name, fd, scale);
The name is whatever you called it in the physics editor. Scale is how much you want to scale it by from the default size. Just use 1 if you don't want to change it.
Can't find any good example of solid wireframe material in Away3d. Maybe I am using wrong tool?
There is a set of Wireframe primitives, like WireframeSphere and WireframeCube. Just define them and add to the scene:
var primitive = new WireframeCube(400,800,400,0xFFFFFF,1);
primitive.x = 1000;
scene.addChild(primitive);
But I believe you want to apply wireframe to non primitive object. In that case - I'm also looking for the answer...
EDIT:
I have found some workaround called WireframeMapGenerator. It's not true wire frame material but at least it works:
http://away3d.com/forum/viewthread/779/#2597