mousedown not working on layer in kineticjs - html

i have been working with a kineticjs game where i have kept one layer for the background, one for the player's character, and others,all together having the same kinetic stage.
Then I ran into a situation where I needed a shape of one layer where i can add multiple events,but though the mouse event are working on the stage they are not getting called on particular shape.I have tried layer.on('mousedown',function(),false);
its not working

try shape.on('mousedown', function().....); as Ani says

Related

Will CacheAsBitmap work on a Movie Clip Object with Frame by Frame Animation?

Hey everyone so I am Using Flash Develop and I have Multiple Move clips added to the stage. Each of course has their own class. Now I have them set up in their class to move across the stage in a Linear motion using a ENTER_FRAME event and coding like so this.x += 10 However these Movie clips each have a frame by frame animation. So I was wondering by adding this.cacheAsBitmap = true; inside the Movie clip objects Class if it would help with the performance or since they have multiple frames if Adobe AIR would have to redraw those frames and cache them as bitmaps hence causing further performance issues. From what Ive read this.cacheAsBitmap = true; will help with performance that way the stage wont have to redraw the movie clip each FPS but has some draw backs like so "You should use cacheAsBitmap only in situation where your vector graphic will remain the same or will have its x or y properties updated."
Any help will be appreciated thanks guys.
You basically got it. cacheAsBitmap won't help (and it will probably hurt) with a frame-by-frame animation.
Using cacheAsBitmap is really only helpful by itself if you are only changing the x,y properties.
Using cacheAsBitmapMatrix (only available in AIR) will let the Flash Player apply transforms (scale, rotation, alpha) with the cached bitmap.
In either case, a frame-by-frame animation is going to force a redraw, which negates the value of both.
As a final note, if you really want to optimize your animation you can try converting it to a sprite sheet.

How do i create an animation in AS3?

I am making a game in AS3 for a school project (using the AIR api). I have about a year's worth of experience in AS3, so i would say i'm proficient but not an expert. Anyway, i have never attempted to do sprite animations in AS3 and i'm not quite sure how to approach it.
If i create a list of bitmaps and call addChild() and removeChild() to display each frame of the animation, it would affect the framerate as these functions are not very efficient (i have tried this before and it tanked my framerate if i had too many animations going at once). I have also tried creating a list of BitmapData objects, adding a bitmap to the display list, and then pointing it to a different BitmapData each frame, but this does not seem to work at all.
So what is the best way to do this? In XNA, for example, i would create a sprite class that draws to the screen using a sprite batch, then i would create a list of sprite objects and cycle through them to create the animation. Is there any way to achieve a similar result in actionscript?
First (simple) method: you can just use multi-frame MovieClip for animation. Put an image in each frame, put a MovieClip on the stage and that's all. You can control this animation using play(), stop(), gotoAndPlay(), gotoAndStop(). It will work without much problems for a generic platform game (I did that myself long ago).
Second (advanced) method: use bitmap blitting. For each animation, create a bitmap image that holds each frame of the animation. Use this image as a source for copying pixels into your current animated object. You just need to copy a particular rectangle area inside a source bitmap that corresponds to the current frame.
The actual blitting happens here
destinationBitmapData.copyPixels(sourceBitmapData, areaRectangle, destinationPoint);
Where destinationBitmapData is your "canvas" that you're blitting to; sourceBitmapData is the source image holding all animation frames; areaRectangle is the Rectangle inside the source image defining which area to copy; destinationPoint is left-top coordinate of the copy area in your canvas.
The destination canvas can be just one small object (like your game character that is moving around) or the entire game screen with all objects. I.e. instead of blitting and adding each object separately, you can just have one big canvas and blit any necessary parts directly to it.
That said, there is already a number of various ready-made engines that use blitting and even advanced techniques like 3D acceleration for 2D sprites.
One of them is Starling.

Away3D rotate scene with mouse

I have a simple scene using the Away3D library and the scene display a simple shape. I'm now dealing with Mouse Events trying to get the effect of rotating the 3D object based on the main coordinates system, but i do not get how to get the initial values when mouse is pressed and what to assign when mouse is moving.
Anyone can help me?
Look at the Intermediate Globe example in the Away3D repository. There is an HoverController class that should have all the functionality you need to handle mouse inputs and rotate the camera around your 3D shape.

AS3 - Using same symbol different image?

Basically I'm working on a new game and have decided to come up with the idea of having different enemies - however would I have to create multiple symbol's for these enemies or can I use that one symbol and change the image that is loaded onto it?
If yes - how would I go about doing this?
Currently the symbol is known as Enemy
Your symbol must be movie clip. In each frame there are different images and added by label. to change the enemy you can call:
Enemy.gotoAndStop(labels)// use Enemy.gotoAndPlay(labels) if you want the animated ones.
with labels is a string.

Drag objects in canvas

Im looking for an easy to use method of assigning drag behavior to multiple objects (images, shapes etc) in canvas. Does anyone have a good way or know of any libraries for dragging objects around? Thanks
Creating your own mouse events takes a little work - ideally you should either create or use some kind of mini-library. I'm thinking of creating something like this in the near future. Anyway, I created a drag and drop demo on jsFiddle showing how to drag images - you can view it here.
You can create draggable images like this:
var myImage = new DragImage(sourcePath, x, y);
Let me know if you have any questions about this. Hope it helps.
EDIT
There was a bug when dragging multiple images. Here is a new version.
Another thing you might want to check out is easeljs it sort of in the style of AS3... mouseEvents dragging etc...
The HTML Canvas—unlike SVG or HTML—uses a non-retained (or immediate) graphics API. This means that when you draw something (like an image) to the canvas no knowledge of that thing remains. The only thing left is pixels on the canvas, blended with all the previous pixels. You can't really drag a subset of pixels; for one thing, the pixels that were 'under' them are gone. What you would have to do is:
Track the mousedown event and see if it's in the 'right' location for dragging. (You'll have to keep track of what images/objects are where and perform mouse hit detection.)
As the user drags the mouse, redraw the entire canvas from scratch, drawing the image in a new location each time based on the offset between the current mouse location and the initial mousedown location.
Some alternatives that I might suggest:
SVG
Pure HTML
Multiple layered canvases, and drag one transparent canvas over another.
The HTML Canvas is good for a lot of things. User interaction with "elements" that appear to be distinct (but are not) is not one of those things.
Update: Here are some examples showing dragging on the canvas:
http://developer.yahoo.com/yui/examples/dragdrop/dd-region.html
http://www.redsquirrel.com/dave/work/interactivecanvas/
http://langexplr.blogspot.com/2008/11/using-canvas-html-element.html
None of these have created a separate library for tracking your shapes for you, however.
KineticJS is one such Javascript Library that u can use exclusively for animations
Heres the Link html5canvastutorials
Canvas and jCanvas
You're definitely gonna want to check out jCanvas. It's a super clean wrapper for Canvas, which kicks open a lot of doors without adding code complexity. It makes things like this a breeze.
For example, here's a little sandbox of something close to what you're after, with dragging and redrawing built right in:
Drawing an Arrow Between Two Elements.
I ventured down the road of doing everything with DIVs and jQuery but it always fell short on interactivity and quality.
Hope that helps others, like me.
JP
As you create new objects whether they are windows, cards, shapes or images to be draggable, you can store them in an array of "objects currently not selected". When you click on them or select them or start dragging them you can remove them from the array of "objects not selected". This way you can control what can move in the event of a particular mousedown event or mousemove event by checking if it isn't selected. If it is selected it will not be in the "not selected" array and you can move the mouse pointer over other shapes while dragging shapes without them becoming dragged.
Creating arrays of objects you would like to drag also helps with hierarchy. Canvas draws the pixels belonging to the foremost object last. So if the objects are in an array you simply switch their instance as in element in the array say from objectArray[20] to objectArray[4] as you iterate through the array and draw the objects stored in the array elements you can change whether other objects are seen on top or behind other objects.