AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt Tutorial 1.5: Move bitch, get outta my way - Simple sprite movement
Thema durchsuchen
Ansicht
Themen-Optionen

Tutorial 1.5: Move bitch, get outta my way - Simple sprite movement

Ein Thema von DP News-Robot · begonnen am 1. Apr 2020
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
15.403 Beiträge
 
#1

Tutorial 1.5: Move bitch, get outta my way - Simple sprite movement

  Alt 1. Apr 2020, 11:40
When looking at the program as it is now, the orb-sprite appears bottom-left, and is fairly small compared to the screen. Why that particular size by the way? Because it is 100 pixels wide, and the (emulator phone) display is 1920 pixels wide, and 1080 pixels tall. Or something. So, it takes about 5% of the screen.


But what is a man gotta do, to make that sprite be elsewhere, say in the center of the screen? Well, let’s draw it on a different {X,Y} position then. Take notice of how X and Y are calculated:
public void render () {
Gdx.gl.glClearColor(0.05f, 0.21f, 0.2f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

// Calculate center pixel position
int screenW = Gdx.graphics.getWidth();
int screenH = Gdx.graphics.getHeight();
int x = screenW / 2 - 50; // -50 from center (sprite is 100 pixels wide)
int y = screenH / 2 - 50;
spr.setPosition( x, y );

// Draw
batch.begin();
spr.draw( batch );
batch.end();
} // render




And how to make it MOVE (over time) or “slide” then? Simple, keep track of either X or Y, and increase (or decrease to move in the opposite direction) a little bit every time it renders. For example:
float offset = 0.f;

@Override
public void render () {
Gdx.gl.glClearColor(0.05f, 0.21f, 0.2f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

// Move
offset += 0.2f;
int screenW = Gdx.graphics.getWidth();
int screenH = Gdx.graphics.getHeight();
int x = screenW / 2 - 50 + (int)(offset);
int y = screenH / 2 - 50;
spr.setPosition( x, y );

// Draw
batch.begin();
spr.draw( batch );
batch.end();
} // render
In case you’re sprite is dropping down instead of moving to the right, bare in mind that screen can be rotated 90 degrees. But anyway, now it moves with 0.2, uh, “things per cycle”… Whatever that is. But hey! It moves!



If we aren’t happy with the size, we can arrange something there as well. Why not try this SINE function, which makes the sprite grow and shrink continuously over time? The function below will size the sprite between 150 and 250 pixels (outcome of sin being a number between -1 and +1).

float offset = 0.f;
float sizer = 0.f;

@Override
public void render () {
Gdx.gl.glClearColor(0.05f, 0.21f, 0.2f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

// Move
offset += 0.2f;
int screenW = Gdx.graphics.getWidth();
int screenH = Gdx.graphics.getHeight();
int x = screenW / 2 - 50 + (int)(offset);
int y = screenH / 2 - 50;
spr.setPosition( x, y );

// Resize
sizer += 0.1f;
spr.setSize( sin(sizer) * 50 + 200, sin(sizer) * 50 + 200 );

// Draw
batch.begin();
spr.draw( batch );
batch.end();
} // render

That was short and very basic, but hey, got to start somewhere right? Next post we’ll be refining these movement and size units. Because now it moves with 0.2 and grows with 0.1…? Whether you are a Metric or Imperial guy, those units don’t make a whole lot of sense to me.



Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:39 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz