lock sprite as moving background with controlled by mouse on GTGE

after 1 day of meditation for finding out. how sprites can be locked in place … finally …. huuh ..a long wait 1 day for finding out the locking of sprites .. hehehe .. look at this.

// JFC
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;

public class LoockedSpriteWithBackgroundMoving extends Game {

    private Background bg;
    private BufferedImage pesawat;
    public Sprite shoot,shoot2,pesawat2,exponend;
    public SpriteGroup pesawate,backgr;
    PlayField playfield;

    public void initResources() {
        bg = new LoopingBackground(getImage("../image/blum_pake/back.jpg"), 50000);
        pesawat      = getImage("../image/plane1.png");
        pesawat2     = new Sprite(pesawat, 275, 250);
        playfield = new PlayField(bg);
        pesawate = playfield.addGroup(new SpriteGroup("pesawat")); 
    public void update(long elapsedTime ) {
        bg.move(0.1 * elapsedTime, 0);
        //that my problem you can include bg with getX
        int tileX = (int) (getMouseX() + bg.getX());
	int tileY = (int) (getMouseY() + bg.getY());
        pesawat2.setLocation(tileX-20, tileY-20);
    public void render(Graphics2D g) {
    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new LoockedSpriteWithBackgroundMoving(), new Dimension(640, 480), false);
/* looping background created by pau_pau */
class LoopingBackground extends Background {
    private BufferedImage backgr;

    public LoopingBackground(BufferedImage backgr, int width) {
        super(width, backgr.getHeight());
        this.backgr = backgr;    
    public void render(Graphics2D g, int xbg, int ybg,int x, int y, int w, int h) {
        int x1 = -(xbg % w);
        int x2 = x1 + backgr.getWidth();
        g.drawImage(backgr, x1, y, null);
        if (x2 < w) {
            g.drawImage(backgr, x2, y, null);

actually that makes difficult the sprite is moved using the mouse. I have tried to render and successfully
but by using its own rendering of the sprites can not be inserted directly into the playfield because we render the sprites. but after finding out way above me .. hahaha see the result is perfect.

thank’s to mr. pau_pau


3 thoughts on “lock sprite as moving background with controlled by mouse on GTGE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s