Processing/4.クラスの利用/4-2.引数付きコンストラクタ
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
#navi(Processing/4.クラスの利用)
* 4-2 引数付きコンストラクタ [#c6a2192e]
前の章の例では、すべてのキャラクタが同じ色であった。new M...
個別に色を設定できるようにしてみよう。それには、Monster( ...
実体を作成すると、コンストラクタの最後の行c=clでプロパテ...
class Monster{
float x, y, dx, dy, d;
int c;
Monster( int cl ){
x = -1;
y = 0;
dx = 0;
dy = 0;
d = 1;
c = cl;
}
void move(){
fill( c );
rect( x, y, 40, 60 );
fill( 255 );
ellipse( x + 15, y + 20, 10, 15 );
ellipse( x + 25, y + 20, 10, 15 );
fill( 0 );
ellipse( x + 15 + dx * d, y + 20 + dy * d, 5, 5 );
ellipse( x + 25 + dx * d, y + 20 + dy * d, 5, 5 );
x = x + dx;
y = y + dy;
if( x<0 || x>width || y<0 || y>height ){
x = random( 10, width-10 );
y = random( 10, height-10 );
dx = floor( random(4) ) - 1.5;
dy = floor( random(4) ) - 1.5;
}
if( frameCount % 60 == 0 ) d = 1;
if( frameCount % 60 == 40 ) d = -1;
}
}
このように変更すると、以前のスケッチのようにm=new Monster...
class Monster{
float x, y, dx, dy, d;
int c;
Monster( int cl ){
x = -1;
y = 0;
dx = 0;
dy = 0;
d = 1;
c = cl;
}
Monster(){
this( #0000f0 );
}
void move(){
...
}
}
ここで、
Monster(){
this( #0000f0 );
}
の部分が引数なしでnew Monster()を実行した場合の処理である...
終了行:
#navi(Processing/4.クラスの利用)
* 4-2 引数付きコンストラクタ [#c6a2192e]
前の章の例では、すべてのキャラクタが同じ色であった。new M...
個別に色を設定できるようにしてみよう。それには、Monster( ...
実体を作成すると、コンストラクタの最後の行c=clでプロパテ...
class Monster{
float x, y, dx, dy, d;
int c;
Monster( int cl ){
x = -1;
y = 0;
dx = 0;
dy = 0;
d = 1;
c = cl;
}
void move(){
fill( c );
rect( x, y, 40, 60 );
fill( 255 );
ellipse( x + 15, y + 20, 10, 15 );
ellipse( x + 25, y + 20, 10, 15 );
fill( 0 );
ellipse( x + 15 + dx * d, y + 20 + dy * d, 5, 5 );
ellipse( x + 25 + dx * d, y + 20 + dy * d, 5, 5 );
x = x + dx;
y = y + dy;
if( x<0 || x>width || y<0 || y>height ){
x = random( 10, width-10 );
y = random( 10, height-10 );
dx = floor( random(4) ) - 1.5;
dy = floor( random(4) ) - 1.5;
}
if( frameCount % 60 == 0 ) d = 1;
if( frameCount % 60 == 40 ) d = -1;
}
}
このように変更すると、以前のスケッチのようにm=new Monster...
class Monster{
float x, y, dx, dy, d;
int c;
Monster( int cl ){
x = -1;
y = 0;
dx = 0;
dy = 0;
d = 1;
c = cl;
}
Monster(){
this( #0000f0 );
}
void move(){
...
}
}
ここで、
Monster(){
this( #0000f0 );
}
の部分が引数なしでnew Monster()を実行した場合の処理である...
ページ名: