I'm currently working on an educational Flash application on the Adobe CS5 Flash Professional platform, using the ActionScript 3 programming language.
In my program the user is required to plot a shape onto a 4x4 squared grid, which I’ve generated using a For Loop that runs through sixteen times.
As the For Loop constructs the grid, it adds 16 child instances of the same 23x23 pixel squared MovieClip, laid out equally in four rows and four columns and each of these MovieClip is assigned a unique ID number ranging from 1-16 and a Mouse Down listener event, ready for user interaction.
Should the user click on a square in the grid, during the course of plotting and selecting their shape, the MovieClip’s colour will firstly change to signify to the user it has been selected for inclusion.
Secondly I’ve also set-up a Boolean based Array [0-15], which links to the corresponding ID numbers of the grid, so for example if I selected the top left square in my 4x4 grid, the [0] property of my Boolean array would change from false to true and likewise if I selected the third square along on the second row of my grid the [6] property of my array would do the same and so on.
Now using this technique and referencing the array, I can always know which blocks have been chosen by my user and by running another For Loop on a subsequent slide in my program, I’ve managed to output, based on my ‘selection’ Boolean array, the same shape that my user has designed, in the confines of another 4x4 grid, with the selected blocks colour changed from grey to black, if any array value is set to True.
Now my problem is that my user will subsequently need to plot their designed shape, produced on the first grid, onto a larger second grid in large volumes (up to 8-9 times on some occasions). This grid is considerably larger than 4x4 one, being 24 x 12 to be exact (288 blocks).
Now what I need is when my user clicks on this second larger grid I want an output of their designed shape to be added to the stage as a brand new MovieClip.
But importantly the shape needs to be cropped down. For example if the user made a square shape on the first grid by selecting blocks 2,3,6,7; I don’t want a 16 block MovieClip (92x92 pixels, based on my 23x23 pixel blocks) being added to the bigger grid with four blocks shaded a different colour, I want a 4 block, squared shape (46 x 46 pixels, based on my 23x23 blocks) to be added, from the top-left grid square you select on the second grid.
Ideally I’d like to create my user’s plotted shape programmatically using the Shape Drawing tool and do this within a function, so I can then run the function each time the user enters the second grid area and update the user’s shape, should the user had gone back and amended it in the first grid. This would also give me the scope to also change the shape colour prior to adding the child of it to my stage, which is another area of functionality I need and another reason why I’d like to do this shape programmatically.
I imagine capturing the x/y co-ordinates of the blocks from the first grid in a further array and subsequently searching through that array to find the first block instance and then draw the shape from this point, could be the way forward but that is as far as my thinking has taken me.
Any ideas or suggested reading on how to do this would be very welcome. Many Thanks.
My suggestion is that you use bitmaps as your base class for such a shape, initialize it with full transparence, then draw() your MCs on that bitmap.bitmapData with adjusted X&Y values, then you place that bitmap over your "larger grid" using supplied X&Y.
var bd:BitmapData=new BitmapData(23*4,23*4,true,0x0);
var mat:Matrix=new Matrix();
mat.tx=-1*LeftCornerX;
mat.ty=-1*LeftCornerY;
for (i=0;i<16;i++) if (BlockMCsSelected[i]) bd.draw(BlockMCs[i],mat);
var bm:Bitmap=new Bitmap(bd);
bm.x=SuppliedX;
bm.y=SuppliedY;
LargerGrid.addChild(bm);
Basically, this should do if you specify your input data correctly. But if you need it to receive events, encapsulate this bitmap into a Sprite object, that one can process events, and give coordinates to sprite, not bitmap.
Related
I'm trying to create a handwriting game with AS3 on Adobe Animate. I've created my board, functions(drawing, erasing, saving, printing and color pannel) so far. But i need to show a score. To do it i thought if i can calculate the percentege of intersection between drawing and a bitmap image(which is my background for now).
Is there any way to do it? Or can you at least tell me with which function should i try that? Thanks a lot.
Note: Here is 2 images from my game. You can easily understand what am i trying to explain and do.
players will try to draw correctly(drawn board)
Empty Board
just a suggestion,
lets assuming that you are recording draw data, a set of points according the frame rate that records mouse positions inside an array.
i used 8 points in my own example, the result would be like this: (6 of 8 = 75% passed)
► black line is correct path(trace btimap) ► red is client draw
we need to search whole of the points array and validate them, so a percentage will be gain easily
how to validate
each point contain x and y, to check if its placed on a black pixel (bitmap trace) we just do
if (bitmapData.getPixel(point.x, point.y) == 0x0) // 0x0 is black
getPixel returns an integer that represents an RGB pixel value from a
BitmapData object at a specific point (x, y). The getPixel() method
returns an unmultiplied pixel value. No alpha information is returned.
Improvment
this practice would be more accurate when there is really more captured points during draw, also the Trace-Bitmap must be like this (above image), not a Dashed (smoothed, styled, ...) Line, however you can use this trace bitmap in background (invisible) and only present a dashed copy of that with a colorful background (like grass and rock textures or any graphical improves) to players.
Note
also define a maximum search size if you need more speed for validating draw. this maximum will be used to ignoring some points, for example if max=5 and we have 10 points, 0,2,4,6,8 can be ignored
I have a pygame screen with multiple tree like objects which grows with each update. I also have to draw random circle which appear and disappear with each update.
I am not able to make the circles alone to disappear. If I try to redraw I loose the slowly incrementing tree structure also. Any help
Using sprite groups (http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group) you can put all the tree like objects into a sprite group designated for trees. Then at the end of the game loop you could blit the background onto the screen and call "tree_group.draw(screen)" to draw all your tree sprites wherever they may be on the screen. With that in place, you can simply draw a random circle wherever you'd like without worrying about taking corrective measures and trying to fix where the circle was drawn previously. Using sprite groups makes updating in game loops a breeze as well. Just call "tree_group.update(args)" and every object in the group will update with the arguments passed in.
I have created a dot matrix slider on a canvas element (much like the sort you get in the stock exchange). Currently I have each letter laid out as an individual matrix and then, through a succession of loops I have these letter converted into one large matrix.
I then go through and draw this matrix column by column up to a maximum amount of columns. The matrix then gets redrawn every X milliseconds, offsetting the viewable area of the matrix by one each iteration until it eventually loops. The large matrix doesn't get redrawn every time.
My issue is that the animation doesn't seem smooth at lower redraw intervals - it jumps about. I've checked the frame rate and it seems fine but occasionally it jumps and I can't work out why!
The function with the settings is right at the bottom of the JSFiddle.
dotMatrix({
animationDelay: 1000,
canvasSelector: '#dot-matrix',
dotRadius: 2,
loop: true
});
Here are some steps you could do:
Prerender each char to an off-screen canvas in a solid color on transparent background. This canvas will be used as a sprite-sheet later. Drawing a rounded rectangle is a relative expensive operation especially when you need x number of it per char.
Set up a gradient for the colors and store that too in an off-screen canvas (by now we can see memory-caching is coming to the rescue..).
Every time you update use requestAnimationFrame instead of setInterval. The latter is not able to synchronize to monitor update. Scroll delay can be calculated using the number of frames elapsed as well as the time since last update (see rAF doc for details).
For each update clear, then "blit" the letter from the sprite-sheet canvas to main canvas. When all are blitted change composite mode to source-atop and blit the gradient canvas on top, and reset composite mode to source-over (don't use save/restore - they are expensive).
In the last step you could also use composite mode copy instead of clearing canvas and using source-over, as that will remove any previous existing pixels for the area you draw to.
Hope this gives you some inputs to improve the performance.
I'm writing a painting application with canvas in HTML5.
I've finished my pencil painting with touching and drawing.
And now i'm trying to make a rectangle. For all the topic i've read, i will have to store all of my finished shape in an array, but if i do that, i will also have to store all the point with normal drawing so that i could draw a rectangle like windows painting.
Please give me another solution to draw rectangle like windows, which old rectangle will be dissappeared and new one will replace before i make a "mouse up".
thanks in advance :)
You will either need to save the previous drawings or use 2 canvases.
If you want to save the previous drawings...
In mousedown:
Save the mouse position (startX/startY).
Set a flag indicating that a drag has started (isDown=true)
In mousemove:
if isDown==false, don't do anything (return)
otherwise, clear the canvas
redraw all your previous drawings (from your saved points array, etc)
draw the current rect from the start to mouse position -- context.strokeRect(startX,startY,mouseX-startX,mouseY-startY)
In mouseup:
clear the drag flag (isDown=false)
If you want to use 2 canvases...
As an alternative to storing every previous drawing, you can use 2 canvases. One canvas is used to draw the current rectangle and a second canvas to keep all the previously draw items Here's an example using 2 canvases so you don't have to store previous drawings: jsfiddle.net/m1erickson/V9J5J/
I am doing some math projects that require a lot of vector line art--that is a line drawn between to points with a circle at the start point and an arrow at the end point. A call to Math.atan2() keeps the arrow aligned. I call this class DrawVectorLineArt() and it creates instances of 2 other custom classes DrawArrow() and DrawCircle().
So far so good--DrawVectorLineArt() draws just what I need. Now I need to animate the vector art.
So in a function onEnterFrame I want to update the postion of arrow and circle, the objects created by DrawArrow() and DrawCircle(), respectively. I also need to clear and redraw the line drawn between them. At this point I am not sure how to proceed in an OOP framework. Do I need to create methods of my custorm class DrawVectorLineArt() to update the position of arrow and circle and subsequently clear and redraw the connecting line?
Any advice or links appreciated. Thanks!
"Do I need to create methods of my custorm class DrawVectorLineArt() to update the position of arrow and circle and subsequently clear and redraw the connecting line?"
Yes.
The arrow and the circle are very members of DrawVectorLineArt, and going by its name and choice of members, so should the line (if it's implemented through actual data). DrawVectorLineArt should contain and implement the whole animation between the circle, arrow, and line. As such, if the animation's supposed to be able to change after creation, the same instance of DrawVectorLineArt should be able to take any two legitimate points supplied to it (or that it becomes aware of internally, depending on what you're doing), reposition the three components, and turn the arrow and line appropriately, within its own code.