cocos2d-js moveTo and animate action - cocos2d-js

I have a moveTo sprite action and I am trying to have the sprite animate while moving. It is a waling animation.
My trouble is I can make the sprite moveTo or animate but not both together so that when the sprite stops moving the animation goes back to the standing frame.
I am using cocos2d-js v3.0
this.sprite = new cc.Sprite.create("#player-stand-f-0");
this.sprite.setPosition(new cc.Point(300,300));
this.addChild(this.sprite);
var animFrames = [];
var str = "";
for (var i = 0; i < 5; i++) {
str = "player-walk-f-" + i;
var spriteFrame = cc.spriteFrameCache.getSpriteFrame(str);
var animFrame = new cc.AnimationFrame();
animFrame.initWithSpriteFrame(spriteFrame, 1, null);
animFrames.push(spriteFrame);
}
var animation = cc.Animation.create(animFrames, 0.025);
var animate = cc.animate(animation);
sprite_action = cc.MoveTo.create(2,cc.p(x,y));
this.sprite.runAction(sprite_action);
this.sprite.runAction(animate);
I have also tried the following but the walking would animate once and not continue until moveTo stops.
var seq = cc.sequence(animate, sprite_action);

If you use "cc.sequence" action it will animate first then move. But if you want to animate the sprite sheet while moving it, there are two ways to achieve that:Look into "cc.Spawn" action. It is used for the purpose just like you need. Another convenient method is to run two actions concurrently.. Below mentioned code will give you the idea.
// create sprite sheet
cc.SpriteFrameCache.getInstance().addSpriteFrames(spritesheet_plist); // add Spritesheet Plist
var SpriteSheet = cc.SpriteBatchNode.create(spritesheet_png); // add Spritesheet Png
this.addChild(SpriteSheet,1);
// Push the frames for animation
var animFrames = [];
for (var i = 0; i < 6; i++) {
var str = "sequence_" + i + ".png";
var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(str);
animFrames.push(frame);
}
// taadaa ...!! Animate the sprites
var animation = cc.Animation.create(animFrames, 0.06);
var sprite = cc.Sprite.createWithSpriteFrameName("sequence_0.png");
sprite.setAnchorPoint(0.5,0.5); // optional
sprite.setScale(1.0,1.0); // optional
sprite.setPosition(widhthPostion, heightPosition);
sprite.runAction(cc.RepeatForever.create(cc.Animate.create(animation)));
SpriteSheet.addChild(sprite,1);
// Move the sprite
var actionMove = cc.MoveTo.create(duration, cc.p(destinationX, destinationY));
sprite.runAction(actionMove);

Related

Adding "SCNNode" to ScreenView to current view of the camera

I've got an image that im trying to add to the current camera location of the Screenview ARSCNView session. Whatever I do it seems to put the image behind the current location of the phone camera and i have to move it back to see it. I have the following code:
var currentFrame = SceneView.Session.CurrentFrame;
if (currentFrame == null) return;
var threeVector = new SCNVector3(currentFrame.Camera.Transform.Column3.X + 0.005f,
currentFrame.Camera.Transform.Column3.Y - 0.02f,
currentFrame.Camera.Transform.Column3.Z - 0.05f);
var scaleFactor = imgToAdd.Size.Width / 0.05;
float width = float.Parse((imgToAdd.Size.Width / scaleFactor).ToString());
float height = float.Parse((imgToAdd.Size.Height / scaleFactor).ToString());
var box = new SCNPlane {
Width = width,
Height = height
};
var cubeNode = new SCNNode {
Position = threeVector,
Geometry = box
};
var mat = new SCNMaterial();
mat.Diffuse.Contents = imgToAdd;
mat.LocksAmbientWithDiffuse = true;
cubeNode.Geometry.Materials = new[] { mat };
SceneView.Scene.RootNode.AddChildNode(cubeNode);
Just trying to execute a simple idea of the ARKit paint apps that you see all over the app store now. So the imgToAdd is a snapshot of the scribble that the user has already put onto the screen and this event is fired at the touchended after an image is created from the view scribbled on.
Any ideas on what I need to change to get it to line up with the current view of the camera on the phone?

how to use getFrames in cocos2d-js?

i am new in cocos2d-js here, i just created this code:`
var that = this;
that._dice
// add player
// create sprite sheet
cc.spriteFrameCache.addSpriteFrames(res.dice_plist);
var spriteSheet = new cc.SpriteBatchNode(res.diceRed_png);
that.addChild(spriteSheet, 2);
// init runningAction
var animFrames = [];
for (var i = 1; i < 7; i++) {
var str = "dieRed" + i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
animFrames.push(frame);
}
var animation = new cc.Animation(animFrames, 0.1);
that.runningAction = new cc.Repeat.create(new cc.Animate(animation), Math.random()*10);
var diceSprite = new cc.Sprite("#dieRed1.png");
diceSprite.visible = true;
console.log(this.getContentSize(diceSprite));
diceSprite.runAction(this.runningAction);
that._dice.push(diceSprite);
var size = cc.winSize;
spriteSheet.setPosition(size.width/6.5, size.height/1.20);
spriteSheet.setAnchorPoint(0.5, 0.5);
spriteSheet.addChild(diceSprite, 2);
`
and i would like to use the getFrames() feature to return the array of ccanimation frames. i'm just thinking to get the information of which picture are being animated on the screen there, for example, if the #dieRed1.png is being animated or visible on the screen, it would show value or return value of 1. i have tried to googled around and cannot find any other clue there. if there is any better method, i would love to see that as well. sorry for the english anyway, a bit confused how to arrange the words. thank you :)
Ok, so refering to
the official docs
{Array} getFrames()
Returns the array of animation frames
Which means you can just do:
var frames = animation.getFrames();
To get the array. And then you'll receive an array of cc.AnimationFrame.
To get sprite link do:
var frame = frames[0];//cc.AnimationFrame
var spriteFrame = frame.getSpriteFrame(); //cc.SpriteFrame
var texture = spriteFrame.getTexture(); // cc.Texture
And the texture itself should have the name attribute which may do the job.

Update the rotation of a CALayer

I am trying to update the current rotation (and sometimes the position) of a CALayer.
What I am trying to in a couple of simple steps:
Store a couple of CALayers in an array, so I can reuse them
Set the anchor point of all CALayers to 0,0.
Draw CALayer objects where the object starts at a position on a circle
The layers are rotated by the same angle as the circle at that position
Update the position and rotation of the CALayer to match new values
Here is a piece of code I have:
lineWidth is the width of a line
self.items is an array containing the CALayer objects
func updateLines() {
var space = 2 * M_PI * Double(circleRadius);
var spaceAvailable = space / (lineWidth)
var visibleItems = [Int]();
var startIndex = items.count - Int(spaceAvailable);
if (startIndex < 0) {
startIndex = 0;
}
for (var i = startIndex; i < self.items.count; i++) {
visibleItems.append(self.items[i]);
}
var circleCenter = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
/* Each line should move up and rotate accordin to this value */
var anglePerLine: CGFloat = (360 / CGFloat(visibleItems.count)).toRadians()
/* Starting position, 270 degrees is on top */
var startAngle: CGFloat = CGFloat(270).toRadians();
/* Lines default rotation, we rotate it to get the right side up */
var lineAngle: CGFloat = CGFloat(180).toRadians();
for (var itemIndex = 0; itemIndex < visibleItems.count; itemIndex++) {
var itemLayer = self.itemLayers[itemIndex];
itemLayer.opacity = 1 - ((0.9 / visibleItems.count) * itemIndex);
/* Calculate start position of layer */
var x = CGFloat(circleRadius) * cos(startAngle) + CGFloat(circleCenter.x);
var y = CGFloat(circleRadius) * sin(startAngle) + CGFloat(circleCenter.y);
var height = CGFloat((arc4random() % 80) + 10);
/* Set position and frame of layer */
itemLayer.frame = CGRectMake(CGFloat(x), CGFloat(y), CGFloat(lineWidth), height);
itemLayer.position = CGPointMake(CGFloat(x), CGFloat(y));
var currentRotation = CGFloat((itemLayer.valueForKeyPath("transform.rotation.z") as NSNumber).floatValue);
var newRotation = lineAngle - currentRotation;
var rotationTransform = CATransform3DRotate(itemLayer.transform, CGFloat(newRotation), 0, 0, 1);
itemLayer.transform = rotationTransform;
lineAngle += anglePerLine;
startAngle += anglePerLine;
}
}
The result of the first run is exactly as I want it to be:
The second run through this code just doesn't update the CALayers correctly and it starts to look like this:
I think it has to do with my code to update the location and transform properties of the CALayer, but whatever I do, it always results in the last picture.
Answered via Twitter: setting frames and transform is mutually exclusive. Happy to help. Finding my login credentials for SO is harder. :D
Found the answer thanks to #iosengineer on Twitter. When setting a position on the CALayer, you do not want to update the frame of the layer, but you want to update the bounds.
Smooth animation FTW

How do I animate a sprite sheet using monotouch and core animation?

The code below is where I am right now, I've made it so I can click a button and change from one sprite on a sprite sheet to another. The fundamentals I'm working out may be crap, I don't really know what I'm doing! By all means, steer me straight if this is so.
What I want to know is how to loop over all the sprites in my sprite sheet. Currently I can just flip between the first and second one (only one time) via button click.
UIImageView test = new UIImageView();
test.Image = UIImage.FromFile("necromancerSpriteSheet.png");
CGImage img = test.Image.CGImage;
CALayer layer = new CALayer();
layer.Contents = img;
layer.Bounds = new RectangleF(0, 0, 128, 128);
layer.Position = new PointF(150, 250);
UIImageView theImage = new UIImageView();
layer.ContentsRect = new RectangleF(0, 0, 0.33f, 0.33f);
layer.RemoveAllAnimations();
theImage.Layer.AddSublayer(layer);
button.TouchUpInside += (s, e) => {
layer.ContentsRect = new RectangleF(0.33f, 0, 0.33f, 0.33f);
layer.RemoveAllAnimations();
theImage.Layer.AddSublayer(layer);
};
I would expect the process would be to have all the sublayers loaded/initialized then simply loop over them in some way. I haven't had any luck figuring out how to achieve this yet though.
Here is my test spritesheet if it helps...
There are many ways of solving this problem, but if you want to use CoreAnimation to drive the animation on the sheet, my suggestion is that you use a keyframe animation (CAKeyFrameAnimation) to drive the animation.
To simplify things, I would merely put all of the items in a single row, and then use something like:
var anim = (CAKeyFrameAnimation) CAKeyFrameAnimation.FromKeyPath ("position.x");
anim.Values = new NSNumber [9];
var times = new NSNumber [9];
for (int i = 0; i < 9; i++){
anim.Values [i] = NSNumber.FromFloat (i*WIDTH);
times [i] = NSNumber.FromFloat (i/9f);
}
anim.CalculationMode = CAKeyFrameAnimation.CalculationDiscrete
You might need to tweak the animation above a little, I did the above without testing.

Flex 4 - Is it possible to animate UITextField

Is it possible to give move effect on a UITextField.
var aminolabel:UITextField;
var aminoLabelMove:Move = new Move(aminolabel);
aminoLabelMove.xFrom = 0;
aminoLabelMove.xTo = 100
aminoLabelMove.duration = 1300;
aminoLabelMove.play();
UITextField could not have animations like move. Which internally calls transformX, transformY. Can have Face effect animation.
Solution
Add UITextField instance to a UIComponent and apply the move effect to the UIComponent.
Example
var aminolabel:UITextField;
var aminoLabelUI:UIComponent;
aminoLabelUI.addChild(aminolabel);
var aminoLabelMove:Move = new Move(aminoLabelUI);
aminoLabelMove.xFrom = 0;
aminoLabelMove.xTo = 100
aminoLabelMove.duration = 1300;
aminoLabelMove.play();

Resources