Commit 3eab8cc5 authored by Lila NICKLER's avatar Lila NICKLER

Avancement sur deplacement du vaisseau

parent debbb0e7
......@@ -2,6 +2,9 @@ package com.imtm1.starwarsgame;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
......@@ -17,11 +20,15 @@ class JoyStickView {
int yDelta;
ViewGroup mainLayout ;
Context context;
Boolean joystickIsPressed = false;
JoyStickView (ViewGroup mainLayout, Context context)
public JoyStickView (ViewGroup mainLayout, Context context)
{
this.mainLayout = mainLayout;
this.context = context;
}
final View.OnTouchListener touchListener = new View.OnTouchListener() {
......@@ -29,28 +36,33 @@ class JoyStickView {
public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
FrameLayout framelayout = new FrameLayout(context);
joystickIsPressed = false;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams laParams = (FrameLayout.LayoutParams) v.getLayoutParams();
xDelta = x - laParams.leftMargin;
yDelta = y - laParams.topMargin;
Log.i("tag", "Hello");
joystickIsPressed = true;
break;
case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v.getLayoutParams();
lParams.leftMargin = x - xDelta;
lParams.topMargin = y - yDelta;
lParams.rightMargin = 0;
lParams.bottomMargin = 0;
joystickIsPressed = true;
v.setLayoutParams(lParams);
break;
case MotionEvent.ACTION_UP:
FrameLayout.LayoutParams layParams = (FrameLayout.LayoutParams) v.getLayoutParams();
Toast.makeText(context, "I'm here!", Toast.LENGTH_SHORT).show();
layParams.gravity = Gravity.CENTER;
//layParams.leftMargin = xDelta;
//layParams.topMargin = yDelta ;
v.setLayoutParams(layParams);
joystickIsPressed = false;
break;
default:
......@@ -62,6 +74,7 @@ class JoyStickView {
}
};
}
......
......@@ -11,9 +11,11 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.PathInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
......@@ -24,6 +26,7 @@ public class MainActivity extends AppCompatActivity {
ImageView padCenter;
ViewGroup mainLayout;
ImageView asteroid1;
ImageView tie;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
......@@ -33,9 +36,9 @@ public class MainActivity extends AppCompatActivity {
padExte = findViewById(R.id.padExte);
padCenter = findViewById(R.id.padCenter);
mainLayout = findViewById(R.id.main);
mainLayout = (RelativeLayout) findViewById(R.id.main);
asteroid1 = findViewById(R.id.asteroid1);
tie = findViewById(R.id.tie);
Path path = new Path();
path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
......@@ -44,18 +47,30 @@ public class MainActivity extends AppCompatActivity {
animator.start();
Runnable runnable = new Runnable() {
JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this);
Handler handlerForMovingTie = new Handler();
Runnable movingTie = new Runnable() {
@Override
public void run() {
JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this);
padCenter.setOnTouchListener(joyStickView.touchListener);
tie.setTranslationX(joyStickView.xDelta);
tie.setTranslationY(joyStickView.yDelta);
Log.i( "TAG","Bool:"+ joyStickView.joystickIsPressed);
if (joyStickView.joystickIsPressed) {
Toast.makeText(MainActivity.this, "joysitck is pressed", Toast.LENGTH_SHORT).show();
handlerForMovingTie.postDelayed(this, 10);
mainLayout.invalidate();
}
}
};
handlerForMovingTie.post(movingTie);
Thread threadTie = new Thread(movingTie);
threadTie.start();
Log.i("TAG", "Thread:"+Thread.currentThread().getName());
Log.i("tag", "Hello");
padCenter.setOnTouchListener(joyStickView.touchListener);
Thread threadJS = new Thread(runnable);
threadJS.start();
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
......@@ -10,11 +10,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" >
android:layout_marginBottom="4dp">
<ImageView
android:id="@+id/fond"
......@@ -26,6 +22,7 @@
app:srcCompat="@drawable/etoilefond" />
<FrameLayout
android:id="@+id/gameSpace"
android:layout_width="match_parent"
android:layout_height="199dp"
android:layout_alignParentStart="true"
......@@ -33,7 +30,8 @@
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="0dp">
android:layout_marginBottom="0dp" >
<ImageView
android:id="@+id/padCenter"
......@@ -90,4 +88,4 @@
app:srcCompat="@drawable/asteroid1" />
</FrameLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
</RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="JoystickView">
<attr name="joystickIsPressed" format="boolean"/>
</declare-styleable>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment