Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Merle

Pages: 1 [2]
16
Graphics / White box glitch when creating multiple objects in a loop
« on: October 10, 2016, 11:12:29 pm »
I know about the white box drawing error when you load things wrong, and I fixed that in previous programs. But I can't figure out how to prevent this from happening when I create multiple objects in a loop and add them to a list. Here is my code:

Platform.h

#ifndef PLATFORM_H
#define PLATFORM_H
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace sf;
using namespace std;

class Platform {
    private:
        string textStr;
        Sprite sprite;
        Texture image;
        Texture t;
    public:
        Platform(string, int, int, int, int);
        Sprite getSprite();
        Texture getTexture();
        string getTextureStr();
};

#endif // PLATFORM_H
 

Platform.cpp

#include "Platform.h"
#include <SFML/Graphics.hpp>

using namespace sf;

//  Default constructor
Platform::Platform(string ima, int x, int y, int w, int h) {
    //  Setting a texture to the path input
    image.loadFromFile(ima);
    //  Setting it so it keeps drawing the texture to the sprite
    image.setRepeated(true);
    //  Setting the private path to the path input
    textStr = ima;
    //  Setting the sprite to the texture, creating the rect and setting the position
    sprite.setTexture(image);
    sprite.setTextureRect(IntRect(x, y, w * 40, h * 40));
    sprite.setPosition(x, y);
}

//  Sprite returning function
Sprite Platform::getSprite() {
    return sprite;
}

//  Texture returning function
Texture Platform::getTexture() {
    return image;
}

string Platform::getTextureStr() {
    return textStr;
}
 

relevant parts of main.cpp

//  Creating a vector that contains all the platforms
vector<Platform> platforms;

//  An array of all the platforms in level 1 - x, y, width and height (in pixels)
int level1[] = {100, 600, 8, 1};

//  Looping through all the ints in the level list, and creating platforms
for (unsigned i = 0; i < (sizeof(level1)/4) - 1; i += 4) {
    platforms.push_back(Platform("brick.png", level1[i], level1[i + 1], level1[i + 2], level1[i + 3]));
}

 

17
Graphics / Re: How to move sprite with keyboard?
« on: October 10, 2016, 11:05:08 pm »
Sprite.move(int x, int y);
 

this function moves the sprite in either direction, so you could say

using namespace sf;

int changeX = 0;
int changeY = 0;
 

and then when you poll for events in the game loop

if (isKeyPressed(Keyboard::A)) {
    changeX = -2;
} else if (isKeyPressed(Keyboard::D) {
    changeX = 2;
} else {
    changeX = 0;
} if (isKeyPressed(Keyboard::W) {
    changeY = -2;
} else if (isKeyPressed(Keyboard::S)) {
    changeY = 2;
} else {
    changeY = 0;
}
 

and when your updating

Sprite.move(changeX, changeY);
 

Pages: 1 [2]
anything