I would like to make my sprite stand still when not moving and animate when keys are pressed. I have a sprite with 8 images and an object that I have created where I assigned the sprite. I have added a key press and key release command and I have added a section where you can put your own custom code. However when I play the game the sprite is animating right away until I press a key and then he stops animating and moves in a still position. How can I fix this?
if image_index = 0
{
image_speed = 0;
}
else
{
image_index = 1;
image_speed = 1;
}
Your answer is in what you posted.
So in other words. If your going to do it this way. Why not just use 3 sprites one for stand one for left and one for right?
of if you insist on not doing that then just set image_index to 0 to start so
Related
So I have this serious problem and I don't know how to let it work. I have this in my step event:
if (image_index >= 6 && image_index <= 8) && (sprite_index == spr_sonia_down_right) {
image_speed = 0;
image_index = 7;
}
The thing that I want is that on the last frame the sprite stops. I have an object and in that object I put the sprites or I change them using sprite_index and that works, but my sprite "down" has a secuence that she is going down and down and down. When she is complete down, that is in the 8 sub image, I want to freeze the sprite on that frame, but I can't reach that.
I think there are two things that you can do to improve this:
Instead of guessing through (image_index >= 6 && image_index <= 8), you can also use the Animation End Event. This Event happens when the object's current sprite has finished it's animation.
I would've made a seperate single sprite that shows the last sprite of the animation, (for example spr_sonia_down_end_right) and show that sprite once the animation is done.
So, for example in Animation End:
if (sprite_index == spr_sonia_down_right) {
sprite_index = spr_sonia_down_end_right;
}
I have found a tutorial on parallax scrolling in spritekit using objective-C though I have been trying to port it to swift without much success, very little in fact.
Parallax Scrolling
Does anyone have any other tutorials or methods of doing parallax scrolling in swift.
This is a SUPER simple way of starting a parallax background. WITH SKACTIONS! I am hoping it helps you understand the basics before moving to a harder but more effective way of coding this.
So I'll start with the code that get a background moving and then you try duplicating the code for the foreground or objects you want to put in your scene.
//declare ground picture. If Your putting this image over the top of another image (use a png file).
var groundImage = SKTexture(imageNamed: "background.jpg")
//make your SKActions that will move the image across the screen. this one goes from right to left.
var moveBackground = SKAction.moveByX(-groundImage.size().width, y: 0, duration: NSTimeInterval(0.01 * groundImage.size().width))
//This resets the image to begin again on the right side.
var resetBackGround = SKAction.moveByX(groundImage.size().width, y: 0, duration: 0.0)
//this moves the image run forever and put the action in the correct sequence.
var moveBackgoundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground, resetBackGround]))
//then run a for loop to make the images line up end to end.
for var i:CGFloat = 0; i<2 + self.frame.size.width / (groundImage.size().width); ++i {
var sprite = SKSpriteNode(texture: groundImage)
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2)
sprite.runAction(moveBackgoundForever)
self.addChild(sprite)
}
}
//once this is done repeat for a forground or other items but them run at a different speed.
/*make sure your pictures line up visually end to end. Just duplicating this code will NOT work as you will see but it is a starting point. hint. if your using items like simple obstructions then using actions to spawns a function that creates that obstruction maybe a good way to go too. If there are more then two separate parallax objects then using an array for those objects would help performance. There are many ways to handle this so my point is simple: If you can't port it from ObjectiveC then rethink it in Swift. Good luck!
I have spent the last few days working on a rubik's cube in Three JS. I am almost done with it, but am having a problem with proper nesting of groups. Basically I have all the cubies made up and when one of the center cubies on a face is clicked, all cubies on that face are added to a group and the group is rotated. The problem is when I try to readd the cubie to the scene they go back to their original location, not the new rotated one. Here is an example if the top face is clicked
else if(cubie.position.x == 0 && cubie.position.y == 12.5 && cubie.position.z == 0){
for(var i = scene.children.length - 1; i >=0; i--){
var cubie = scene.children[i];
if(cubie.position.y == 12.5){
group.add(cubie);
}
}
group.rotateOnAxis(new THREE.Vector3(0,-1,0), Math.PI/2);
}
and here is where I try to readd the cubie back to the scene
for(var i = group.children.length - 1; i >= 0; i--){
scene.add(group.children[i]);
}
scene.remove(group);
if I comment out both lines, then one complete rotation appears, but since the cubies are now apart of group I cant rotate them again. If I remove the first line, and leave remove(group) then obviously the row disappears, but if I leave both lines in, then the cube doesn't change at all as if when I readd the cubie to the scene it takes its original place back...I have tried everything I can think of. Any help would be greatly appreciated
I will post the entire code to pastebin if anyone needs it.
http://pastebin.com/vj6LgR0r
I have some images/sprites/buttons (i tried them all :)) scrolling/moving on the stage. The user has to tap them to remove them.
The problem is that the touchevent does not match the position of the image.
The faster the touchable sprites move, the further the distance between the touch and the actual image. i.e.:
a image is moving across the screen with 20px/frame:
Touching the image triggers nothing, touching 20 before it triggers a touch on the image.
when image is not moving, this doesn't happen. It seems that the image is already moved internally, but not yet drawn to the screen, but thats just a wild guess. The example below uses a button, but the same goes for an image. I"ll provide a short example of the code, but i guess its pretty straightforward what i'm trying to do.
private var _image:Button;
protected function init():void {
//create pickup image
_image = new Button(assets.getTexture("button"));
_image.scaleWhenDown = 1;
_image.addEventListener(Event.TRIGGERED, onClick_image);
addChild(_image);
//listen to touch event (not used, but example for touch on image instead of button
//touchable = true;
//addEventListener(TouchEvent.TOUCH, onTouch_image);
}
private function onEnter_frame(e:Event):void {
_image.x -= 20;
}
I am having problems with the following piece of code:
def handle_image_flip(self, old_rect):
new_rect = self.last_frame.get_rect()
self.owner.rect = new_rect
self.owner.rect.y = old_rect.y
if self.owner.facing == -1:
self.owner.rect.right = old_rect.right
else:
self.owner.rect.x = old_rect.x
def animate(self, tick):
if tick - self.last_update > self.image_info[self.action]["frame_rate"]:
self.last_frame = self.get_next_frame(tick)
old_rect = self.owner.rect.copy()
self.owner.image = self.last_frame
self.handle_image_flip(old_rect)
self.last_update = tick
Where:
self.owner is the sprite this piece of code handles
self.owner.facing is the direction the sprite is facing
self.last_frame is the new image I want to display
Since the sprites have different widths, I am getting glitchy animations when facing
LEFT (-1).
There are no problems when moving RIGHT whatsoever.
Any ideas?
If you track player's location using Rect.center or Rect.centerx vs default of topleft then different widths can work.
Depending on your game, using sprite sheets may be beneficial.
in your if statement, in one case you change self.owner.rect.right, in the other case you change self.owner.rect.x.
This could be part of the problem since when you are moving left, you are changing .right rather than .x