Ok so im writing 2d game like bubble-bobble i have sprites i have done the physics (moving,jumping,falling so far :D) i have done some kind of shooting but now i want to make my player sprite(rectangle image) animated,but i dont want some kind of very complicated animation. I want something like this : i have 2 player images Right faced creature and Left faced creature the rectangle size of the images is exactly the same ,i also have :
Vector2 playerPosition declared
player = Content.Load<Texture2D>("PlayerRight"); in my LoadContent
spriteBatch.Draw(player, playerPosition, Color.White); in my draw function
all i want is when i press Right/Left - arrow buttons my player image switch between
Right/Left faced images
i dont know what kind of code and where i have to write for that task.
if you need more information about the task please let me know
this is not best solution, but it could give you hint.
playerRight = Content.Load<Texture2D>("PlayerRight");
playerLeft = Content.Load<Texture2D>("PlayerLeft");
playerCameraFacing = Content.Load<Texture2D>("playerCameraFacing");
declare PlayerSprite as Texture2D in player class
update funciton pseudo:
PlayerSprite = playerCameraFacing ;
if key.left then PlayerSprite = playerLeft;
if key.right then PlayerSprite = playerRight;
draw function pseudo:
draw(PlayerSprite, Position, Color)
i put all my animation frames into single image, and then show them using "sourceRectangle".
what is source rectangle in spritebatch.draw in xna
http://msdn.microsoft.com/en-us/library/ff433988.aspx
Related
I create a macOS single window application and add a Sprite Particle System file with template Stars. and the visual effect just like this:
And I want to add it to my viewController, as the guide of this answer, I got the result like this, and it was not which I desired:
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
let particlesNode = SCNNode()
let particleSystem = SCNParticleSystem(named: "Welcome", inDirectory: "")
particlesNode.addParticleSystem(particleSystem!)
scene.rootNode.addChildNode(particlesNode)
skView.backgroundColor = .black
skView.scene = scene
}
So, I'm wondering what's wrong and what should I do?
Here is the demo repo: Link Here
The particle system itself is the standard "star" SceneKit particle system available in Xcode, with no changes.
Well I made a little progress. If I swivel the camera around 180 degrees, I can see the stars receding, so we can tell that the particle system is running ok. In the default orientation, though, all I saw was blinking lights. So I think the particles are being generated with a Z position of 0, the same as the camera's.
If I move the system's node away from the camera
particlesNode.position = SCNVector3(0, 0, -20)
I still just see blinking lights. But if I click on the SCNView, the animation works correctly. I see stars coming at me.
I don't understand why I have to click the view to get it to work right. I tried isPlaying = true but that made no difference.
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've read several other posts about rotating an image around the center point, but I've yet to figure it out. I even copy pasted one of the solutions posted on another SO question and it didn't work.
This is my code
def rotate(self, angle):
self.old_center = self.surface.get_rect().center
self.surface = pygame.transform.rotate(self.surface, angle)
self.surface.get_rect(center = self.old_center)
it's inside a class which contains the surface.
When I call this method the image rotates but also translates, and gets distorted.
You are not assigning the new rect, you should do something like:
self.rect = self.surface.get_rect(center = self.old_center)
And you should always keep the original surface and rotate from the original, that way distortion doesn't accumulate when rotating multiple times.
Update
If you don't want to keep track of the rect object, you can do it every time you blit. If you keep the center coordinates.
Example:
rect = object.surface.get_rect()
rect.center = object.center
display.blit(object.surface, rect.topleft)
I think this picture better explains my problem: http://i48.tinypic.com/wvrbcy.png
On this picture the camera is moving along the ellipse. The code I have for this trajectory is:
var r = 0;
function render() {
cameraMain.rotation.y+=0.003;
cameraMain.position.x = Math.sin(r*0.1)*500;
cameraMain.position.z = Math.cos(r*0.1)*1000;
r+=Math.PI/180*2;
}
But that's not what I need. I want my camera to move in a kinda star trajectory, so that it'll have kinda 'zoom in' / 'zoom out' effect. How can I accomplish this?
You want to google "parametric equation" for "astroid", or "deltoid curve", or "superellipse".
Here are some links where you can get the kinds of parametric equations you are looking for:
Astroid: http://en.wikipedia.org/wiki/Astroid
Deltoid Curve: http://en.wikipedia.org/wiki/Deltoid_curve
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