how to make sprites stick together in game maker? - game-maker

I don't know how to get sprites to stick to each other so they become one big object instead of tiny little pieces, for example:
attaching a thruster to a box, then the thruster stays in that spot while pushing the box, and also is there a certain term for what I'm talking about?

You could also attach all parts to one of the objects, it would sort of look like:
//Main object
x = 5;
y = 20;
//other object step event
x = obj_main.x + <any value to put it where you want>;
y = obj_main.y + <any value to put it where you want>;
//This will force the parts to follow the main object.
`
`

You can use an array, defined in the 'main' object to use a sort of grid to define where each piece is and then either draw each individual sprite, based on its position in the array, originating from the 'main' object's coordinates. Or just create an individual instance of an object if you would like to have additional functionality by trading off some performance.
For more information on arrays and how to position sprites and objects based on set coordinates, check out the GML documentation provided below:
Arrays:
https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml%20language%20overview/401_06_arrays.html
lengthdir:
https://docs.yoyogames.com/source/dadiospice/002_reference/maths/real%20valued%20functions/lengthdir_x.html

what I did was make the object disabled, so when I press left and right it doesn't go anywhere, only the other piece would move, but when it came into contact it allowed the other piece to move along with it, and set its speed to the corresponding objects speed, in simpler term, when I collide with it, it turns the movement on and goes in the same direction as the current object in the same speed, making it look like its sticking

Related

Predefine Layout using Vis.js network

I would like to know if there is a way to inject a predefine layout in Vis.
I managed to save all coordinate of my nodes (X : Y) when i drag an drop each node, which is then saved to the database with a specified ID for each nodes.
What i struggle with is to specified this dataset to vis when i initialize a map with vis ( here is the doc of layout initilisation : http://visjs.org/docs/network/layout.html#)
i would like to put an array with my id nodes and position X Y so that it get saved when user change their layout.
It appears that it is not possible, but maybe there is a hidden way ?
Thanks in advance
This is quite possible:
to set initial layout, just add coordinates to each node (and disable physics so that they don't flow away from their positions):
nodes = [{id:1, label:'some', x:100, y:0 }, ... ];
options = { physics: false, ... };
to get current coordinates, use network.getPositions()
to save those at layout change, you probably want to use the dragEnd event and the on method (use network.getPositions() inside the event handler)
You can opt my implementation in the VisGraphPlugin repo (it's a plugin for TiddlyWiki Classic), just look for dragEnd and saveDataAndOptions, the latter may be of interest.
Instead of saving positions and id's you can simply use getSeed() method to save your layout configuration in a seed. Then, when you start the network again you can load this seed into the layout.randomSeed to have the same configuration.
The documentation for getSeed() says:
If you like the layout of your network and would like it to start in the same way next time, ask for the seed using this method and put it in the layout.randomSeed option.

Coloring a Shapefile map in ASP.NET

I have a Shapefile map of Maryland separated by zipcode. What I'd like to do is color each area based on a value I'm looking up in a database. Currently, I'm using ASP.NET with the SharpMap package. The basic questions are:
1) How do I associate a shape with its zipcode? I can generate the list of zipcodes using SharpMap's ExecuteIntersectionQuery with the BoundingBox set as the extent of the map, but I have no idea how to then connect each row of the resulting table with the shape it represents.
2) Once I have access to an individual shape and know what color I want, how do I assign the color to the shape? In SharpMap, I can color a VectorLayer, but a VectorLayer is generated from a source .shp file, not a shape.
I'm open to using other free map packages besides SharpMap (so no ArcGIS), but for legal reasons I can't use GoogleMaps.
I feel like this should be relatively simple, but trying to find any decent resource for SharpMap is pretty difficult.
EDIT: Alright, I've made a lot of process just by reading over what documentation there is. By setting the FilterDelegate of ShapeFile, I can make a layer consist of only the rows where the zip code matches a certain value. Now my only problem is making the delegate filter look for a different zip code each time. Can I pass another parameter besides the FeatureDataRow? Should I resort to a global variable?
You'll need to use thematics to do this.
I'll assume you already have some code that configures your map and it's layers. Your shapefile is a VectorLayer.
VectorLayer shapefileLayer = GetMyShapefileLayer();
shapefileLayer.Theme = new SharpMap.Rendering.Thematics.CustomTheme(GetStyleForShape);
Then, method GetStyleForShape is called every time the map needs a style for rendering. It looks like this:-
private SharpMap.Styles.VectorStyle GetStyleForShape(SharpMap.Data.FeatureDataRow row, SharpMap.Layers.Layer layer)
{
}
In the method, you create and return a VectorStyle. The table data associated with the feature it's trying to render is passed as a parameter to this method.
So you can use the row parameter to get your zip code, do whatever logic you need to do to calculate it's style, configure that style and return it.
This method (potentially) gets called a lot, so consider storing the styles and reusing them, rather than recreating them every time.
Hope this helps.

Initializing multiple subclassed sprites of the same kind in Cocos2D from Tiled

I'm trying to figure out the best way to initialize multiple sprites placed in Tiled that are the same type. I don't even know really how to start off in Tiled, because I'm only used to initializing one sprite by filling out the name field, and then in my CCLayer with the map I do something like
CCTMXObjectGroup *objects = [theMap objectGroupNamed:#"oj"];
NSMutableDictionary *startPoint = [objects objectNamed:#"StartPoint"];
int x = [[startPoint valueForKey:#"x"] intValue];
int y = [[startPoint valueForKey:#"y"] intValue];
hero = [[HeroClass alloc] init];
[self addChild:hero.heroSpriteSheet];
hero.heroSprite.position = ccp(x,y);
As you can see, my "hero" is subclassed, and this goes for another sprite that is a bird, but I want an x amount of them. I import the header of HeroClass and create a pointer, make a property and synthesize that in my main file and finally release HeroClass in dealloc. I have a feeling that I will most likely end up use NSMutableArray, but I don't really know how in the case of subclassing. Also I'm not sure if I'll be using objectNamed:#"name" because I'll have x number of these sprites that I will be just duplicating throughout Tiled.
There's a helpful post here that shows how to add multiple sprites, but I don't know how to relate it to Tiled or subclassing:
http://www.iphonedevsdk.com/forum/iphone-sdk-game-development/58334-adding-sprites-screen-when-detecting-tap-cocos2d.html
Does anyone have any useful pointers or code examples relating how to resolve my issue? Thanks!
TLDR:
How do I initialize sprites from the same class in a CCLayer (that renders my map and other sprites) based upon definite object locations that were placed in a Tiled TMX map?
I'd suggesting creating an enemy class. Then, you create a bunch of them using a for loop. As far as spawnpoints, you would have each enemy set its own spawn point up based on rules. (Map borders, "safe areas", etc.)

Flash/Flex: "Warning: filter will not render" problem

In my flex application, I have a custom TitleWindow that pops up in modal fashion. When I resize the browser window, I get this warning:
Warning: Filter will not render. The DisplayObject’s filtered dimensions (1286, 107374879) are too large to be drawn.
Clearly, I have nothing set with a height of 107374879.
After that, any time I mouse over anything in the Flash Player (v. 10), the CPU churns at 100%. When I close the TitleWindow, the problem subsides. Sadly, the warning doesn't seem to indicate which DisplayObject object is too large to draw. I've tried attaching explicit height/widths to the TitleWindow and the components within, but still no luck.
[Edit]
The plot thickens:
I found that the problem only occures when I set the PopUpManager's createPopUp modal parameter to "true." I don't see the behavior when modal is set to "false." It's failing while applying the graying filter to other components that comes from being modal. Any ideas how I can track down the one object that has not been initialized but is being filter during the modal phase?
Thanks for reading.
This might not be the case in your application, but I have come across similar cases where a height or width of an object has been set to some unreasonable big number as the result of misuse of an unsigned integer in calculations for positioning, height or width.
Schematic code for such a scenario could be like this:
var offset:uint = 30;
var position:uint = txt.textHeight - offset;
divider.y = position;
The code wrongfully assumes that txt.textHeight will always be bigger than 30. If it is not, txt.textHeight - offset will be a negative number, that when stored in an uint will instead become a very large number.
Let's say for example that the text in txt, presumed to be a long multiline text, instead is a single line that is 20 pixels heigh. The result will then be -10, and when stored in the uint var position, the value of position will be 4294967286.
The above is crappy code, an example, but in a real world situation the uint problem can occur in some more complex way, that might be harder to spot right away. I guess it is seldom a good idea to use an unsigned integer for stuff like x and y positions, that can have negative values.
You could write some code to recursively step down the hierarchy of DisplayObjectContainer and DisplayObject objects and check for the large height.
Should be pretty simple to write. A function something like this should do the trick:
function RecurseDisplayObjects(DisplayObject obj):void
{
//check for height and do a trace() or whatever here
if(obj is DisplayObjectContainer)
{
var container:DisplayObjectContainer = obj as DisplayObjectContainer;
for(var i:int=0; i<container.numChildren; i++)
{
RecurseDisplayObjects(container.getChildAt(i);
}
}
}
You would need to start this off by passing it the top level DisplayObject in your application. (possibly obtained with DisplayObject.root)
The other option you have is to get the Flex framework source and modify it to give you a more meaningful error.
The problem is probably not in your TitleWindow, but in objects below it. The filter failing to render is probably the blur filter flash applies over everything below the modal dialog. If one of the objects on the stage is too big to apply blur on it in real time, you get the error you mentioned.
I solved that problem by applying a mask to the object below the titlewindow, set to the size of the stage. That will probably solve your problem but you should definitely look into why something gets to that size, doesn't sound healthy. :-)
Sadly I have no idea, but we're trying to track down a similar issue in ours. Maybe this will help?
http://www.mail-archive.com/flashcoders#chattyfig.figleaf.com/msg48091.html
I had a similar issue, tracked it down to an alpha filter applied to an object scaled to -0.23453422334. Once I rounded the scale to 2 significant digits everything worked fine. A difficult error to track down however.

Editing A Library Symbol From ActionScript

In the Flash authoring environment I can edit a library symbol and all on-stage instances based upon it reflect the changes. How can I do the same thing in ActionScript? There seems to be no way to address a library symbol.
For example:
Inside Flash CS3, I have created a Square.swf file that has 100 instances of the library symbol Square.
Now, Square.swf is loaded into another file BlueSquare.swf and I want to change the Square symbol into a blue square so that all instances of Square become blue.
How do I do this using Actionscript?
Thanks for the help.
What's in a clip's library symbol is the author-time definition of that object - you can't change it at runtime. Instead the normal approach would be to dynamically change the contents (not definitions) of the clips you want to change, which can be done in various ways, but all the good ways of doing that involve making the dynamically-changing clip understand how to update its appearance. So you need to be able to re-author the changing clips to suit your needs.
If you're loading in an animation that somebody else made, and trying to go through and replace all instances of object A with object B, the only way to achieve that is to traverse through the content's display list looking for A, and when you find one, remove its children and replace them with the the contents of a B. Mind you, for animations that may not really solve your problem, since animations normally add and remove clips frequently, so at any given point you could replace all the "hand" clips with "hand2", but then a frame later new "hand" clips might come into existence. But short of opening up the SWF and changing the binary data inside, there's no other way to dynamically change all of a given object to something else unless the object knows how to change its contents.
If it is only about making sure that the square you are attaching is blue you could use the colorTransform to change its appearance:
var someSquare:Square = new Square();
someSquare.transform.colorTransform = new ColorTransform(0,0,0,1,0x00,0x00,0xff,0x00 );
addChild( someSquare );
Of course this does not change the color of all instances that you have already attached.
If you really wanted to change the actual SWF symbol in Actionscript the only way I see is to parse the swf with as3swf ( https://github.com/claus/as3swf/wiki ), find the shape tag of the symbol, change it and then load the ByteArray that contains the swf via loader.loadBytes() - but that's admittedly quite a complicated way and you can achieve the same result by simply putting some colorizing code into the shape symbol itself and then trigger the color change via an Event that is broadcasted by your main app.
Of course, if you make custom component, when you change it changes will appear on all instances of that component/class. Here's the example: http://livedocs.adobe.com/flex/3/html/intro_3.html
On the other hand, if you use modules whey pretty much do the same as swf-s you used in Flash, when you rebuild-recompile them changes will reflect on your main application which uses them. Here's th eexample for modules: http://blog.flexexamples.com/2007/08/06/building-a-simple-flex-module/
So MXML/AS component/class are your "symbols" which you can create or drop on stage on fly.
Modules are "movies" you can load and they run on their own with possibility to communicate to main movie.
The closest way of achieving this is to use Bitmaps. If you update the bitmapData they display, they will all update automatically.
However this approach is not good at all. You should maintain application state separately in an object model, and have the visualisation update, if the state changes.
What you want to do, is to misuse a feature for changing graphic appearence at design time, to change application state at runtime. In generally, ideas like these can be thought off as bad.
For example if you take the time to separate the state model and the visualisation layer, it will become fairly easy to save the game state on a server or to synchronize it with other clients to achieve multiuser features.
greetz
back2dos
If you are trying to build an Avatar and user can customize your Avatar parts e.g. hands, legs, face etc. and you want all these assets to be kept in separate swf file, that is pretty straightforward. You keep all the assets, in separate swf or one large swf file and load them at runtime. Now, maintain your Avatar object instance and place the child objects, which are chosen by the user.
You can create inside your class a static List with references all the created instances and then apply a change with static methods. For example:
package
{
import flash.display.MovieClip;
import flash.geom.ColorTransform;
public class Square extends MovieClip
{
public static var instances:Array = new Array();
public function Square():void
{
Square.instances.push(this); // This is the trick. Every time a square is created, it's inserted in the static list.
}
// This property gets the color of the current object (that will be the same of all others because the setter defined below).
public function get color():ColorTransform
{
return this.transform.colorTransform;
}
public function set color(arg:ColorTransform):void
{
// Sets the color transform of all Square instances created.
for each(var sqr:Square in Square.instances)
{
sqr.transform.colorTransform = arg;
}
}
}
}

Resources